您的位置:首页 > 其它

相对布局(RelativeLayout)

2016-05-22 10:44 253 查看
相对布局:

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

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

        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>

效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息