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 离某元素上边缘的距离
还是先简单一个示例
新建5个按钮,未进行任何属性设置,我们可以清楚看到5个按钮都在重叠在左上角,这就是相对布局,必须对相对布局中的每个VIEW设置相对属性才能准确控制每个VIEW的位置,达到灵活的效果。
我们使用几个属性试试
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,我们使用相对布局尝试下该效果。
效果如下:
此布局中的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>
效果如下:
相关文章推荐
- 那些年Android开发中遇到的坑
- 那些年Android开发中遇到的坑
- Android5.0如何预置apk
- Android开发之使用AlertDialog创建对话框,单选框和多选框
- 下一代Android渠道打包工具
- (android:windowIsTranslucent)影响(android:windowAnimationStyle)Activity切换动画无效
- 【Android图片加载框架】制作自己的图片加载框架(一)
- 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation
- Android获取存储卡路径的方式
- Android编程中出现The connection to adb is down问题的解决方法
- android 设置边框只显示部分线条
- 《Android群英传》读书笔记(4)第五章:Android Scroll分析
- LayoutInflater和findViewById的区别
- android中 menu的属性详细解释
- 丢失Android系统库或者Conversion to Dalvik format failed with error 1错误的解决方法
- Android-图片存入系统相册更新显示
- Android WebView中获取网页的title (包括调用goback)
- Android Studio系列教程六--Gradle多渠道打包
- Android编程之退出整个应用程序的方法
- android 优化显示图片有效避免oom