相对布局(RelativeLayout)

来源:互联网 发布:淘宝2元包邮怎么赚钱 编辑:程序博客网 时间:2024/06/16 02:45




相对布局:

            特点,所有的控件都是以相对的位置去放置





相对布局需要的关键词汇:



        android:layout_centerHorizontal="true"     //水平居中
        android:layout_centerVertical="true"         //竖直居中

        android:layout_centerInParent="true"       //中心居中

        android:layout_alignParentLeft="true"       //左
        android:layout_alignParentTop="true"       //上

       android:layout_alignParentRight="true"      //右
        android:layout_alignParentBottom="true"  //下

练习代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中"
        android:layout_centerHorizontal="true"   //水平居中
        android:layout_centerVertical="true"     //竖直居中
        android:layout_centerInParent="true"     //中心居中

        android:id="@+id/millde"  //添加id
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上"
        android:layout_above="@id/millde"//使用id,在此id表示的button上
       android:layout_centerHorizontal="true"
            
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下"
        android:layout_below="@id/millde"//使用id,在此id表示的button下
       android:layout_centerInParent="true"
            
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左"
        android:layout_toLeftOf="@id/millde"//使用id,在此id表示的button左
        
       android:layout_centerVertical="true"
            
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右"
        android:layout_toRightOf="@id/millde"//使用id,在此id表示的button右
       android:layout_centerVertical="true"
            
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左上"
        android:layout_alignParentLeft="true"//左
        android:layout_alignParentTop="true"//上

      />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左下"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"//下
            
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右下"
        android:layout_alignParentRight="true"//右
        android:layout_alignParentBottom="true"
        
            
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右上"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        
            
      />
    
    

</RelativeLayout>


效果:

0 0