您的位置:首页 > 移动开发 > Android开发

Android布局之RelativeLayout点滴

2015-12-16 11:58 435 查看
继续学习布局中的RelativeLayout,相对布局

此布局中的VIEW位置都是相对的,可以相对于父VIEW,也可以相对于其他同级VIEW。

网上摘抄一下属性如下:

  a)、第一类:属性值为true或false

  android:layout_centerHrizontal 水平居中

  android:layout_centerVertical 垂直居中

  android:layout_centerInparent 相对于父元素完全居中

  android:layout_alignParentBottom 贴紧父元素的下边缘

  android:layout_alignParentLeft 贴紧父元素的左边缘

  android:layout_alignParentRight 贴紧父元素的右边缘

  android:layout_alignParentTop 贴紧父元素的上边缘  

  b)、第二类:属性值必须为id的引用名“@id/id-name

  android:layout_below 在某元素的下方

  android:layout_above 在某元素的的上方

  android:layout_toLeftOf 在某元素的左边

  android:layout_toRightOf 在某元素的右边

  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐

  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐

  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐

  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

  c)、第三类:属性值为具体的像素值,如30dip,40px

  android:layout_marginBottom 离某元素底边缘的距离

  android:layout_marginLeft 离某元素左边缘的距离

  android:layout_marginRight 离某元素右边缘的距离

  android:layout_marginTop 离某元素上边缘的距离

还是先简单一个示例

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_title_container"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#ffffff"
android:gravity="center_vertical"
>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button5" />
</RelativeLayout>




新建5个按钮,未进行任何属性设置,我们可以清楚看到5个按钮都在重叠在左上角,这就是相对布局,必须对相对布局中的每个VIEW设置相对属性才能准确控制每个VIEW的位置,达到灵活的效果。

我们使用几个属性试试

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_title_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center_vertical"
>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_centerInParent="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_above="@id/button1"
android:layout_alignLeft="@id/button1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
android:layout_below="@id/button1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:layout_above="@id/button1"
android:text="button4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:layout_below="@id/button1"
android:text="button5" />
</RelativeLayout>




button1:我们使用了android:layout_centerInParent="true" ,代表在父VIEW的中间显示

button2:android:layout_above="@id/button1"和android:layout_alignLeft="@id/button1",代表在button1的上边,且左边缘和button1对齐

button3:andoird:layout_below="@id/button1",代表在button1的下边,且默认居左对齐

button4:android:layout_toRightOf="@id/button1"和android:layout_above="@id/button1",代表在button1的上边,且右边缘和button1对齐

button5:android:layout_toRightOf="@id/button1"和android:layout_below="@id/button1",代表在button1的下边,且右边缘和button1对齐

其他属性自行尝试效果吧。

最终示例,我们见很多app标题栏为左右个一个按钮,首页、返回,中间为app title,我们使用相对布局尝试下该效果。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_title_container"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#ff00ff"
android:gravity="center_vertical" >

<TextView
android:id="@+id/tvBack"
android:layout_width="wrap_content"
android:layout_height="20dip"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dip"
android:layout_marginLeft="5dip"
android:text="back"
android:layout_centerVertical="true" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tvBack"
android:layout_toLeftOf="@+id/tvHome"
android:gravity="center_horizontal"
android:layout_marginTop="5dip">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is title"
android:textColor="#ffff00"
android:textSize="30dip" />
</LinearLayout>

<TextView
android:id="@+id/tvHome"
android:text="Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:layout_centerVertical="true"/>

</RelativeLayout>


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