Android中RelativeLayout的使用-----------转载下----以免忘记.
来源:互联网 发布:android程序员简历 编辑:程序博客网 时间:2024/05/08 11:19
Android中RelativeLayout的使用
RelativeLayout是一种相对布局,它是依靠与父容器,以及同一容器中其它控件的相对位置来排列显示的。RelativeLayout是实际布局中最常用的布局方式之一(本人最常用),它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。
默认情况下RelativeLayout中的子元素是以左上角对齐父元素显示,并且后面添加的元素将会以左上角为基点覆盖前面添加进去的子元素。
首先列出RelativeLayout的一些常用属性:
第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素居中
android:layout_alignParentBottom 位于父元素的最下边
android:layout_alignParentLeft 位于父元素的最左边
android:layout_alignParentRight 位于父元素的最右边
android:layout_alignParentTop 位于父元素的最上边
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照
第二类:属性值必须为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 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
代码演示,首先看效果图:
这里Activity的布局为RelativeLayout,并且在RelativeLayout的左上角、右上角、左下角、右下角、水平居中、垂直居中、居中分别添加一个TextView,另外,以居中的TextView为基点,分别在该TextView的上下左右添加一个TextView。布局文件如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><TextView android:layout_width="100dip" android:layout_height="100dip" android:background="@drawable/bg_text" android:gravity="center" android:text="@string/txt_default" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/left_top" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="@string/right_top" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:text="@string/left_bottom" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:text="@string/right_bottom" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_centerHorizontal="true" android:text="@string/center_h" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_centerVertical="true" android:text="@string/center_v" /><TextView android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_centerInParent="true" android:layout_margin="20dip" android:text="@string/center" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_toLeftOf="@id/center" android:layout_alignBottom="@id/center" android:text="@string/center_left" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_above="@id/center" android:layout_alignLeft="@id/center" android:text="@string/center_above" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_toRightOf="@id/center" android:layout_alignBottom="@id/center" android:text="@string/center_right" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:layout_below="@id/center" android:layout_alignLeft="@id/center" android:text="@string/center_blew" /></RelativeLayout>
注意:
1.在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。
2.不能在RelativeLayout容器本身和他的子元素之间产生循环依赖,比如说,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的位置设置为android:layout_alignParentBottom="true"。
另外这里再说下如何通过代码动态实现RelativeLayout:
先看截图:
代码实现RelativeLayout的布局,添加一个TextView并使得它相对RelativeLayout居中显示
/** onCreate():代码实现RelativeLayout的布局,添加一个TextView并使得它相对RelativeLayout居中显示 **/ //实例化一个RelativeLayout RelativeLayout mRelativeLayout = new RelativeLayout(this); //创建一个TextView,并设置对应的LayoutParams使该TextView居中于父元素 TextView mTextView = new TextView(this); mTextView.setText("代码实现RelativeLayout的布局"); RelativeLayout.LayoutParams txtLp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); txtLp.addRule(RelativeLayout.CENTER_IN_PARENT); //居中 //将TextView添加进RelativeLayout mRelativeLayout.addView(mTextView, txtLp); //将RelativeLayout该布局设置给Activity setContentView(mRelativeLayout);
点击这里下载相关代码:RelativeLayoutTest
- Android中RelativeLayout的使用-----------转载下----以免忘记.
- Android中RelativeLayout的使用
- Android中RelativeLayout的使用
- 部署有用,先记录下以免忘记
- android部分知识,自己记住以免忘记
- 收集一些很好的网站,以免忘记
- android中使用RelativeLayout布局
- Android中LinearLayout、RelativeLayout、TableLayout、AbsoluteLayout容器的使用
- Android中LinearLayout、RelativeLayout、TableLayout、AbsoluteLayout容器的使用
- 常用用的header状态值,记录下来以免忘记
- 最近在使用swift写代码时碰到block引用循环的问题。记下一笔以免忘记。
- Android中LinearLayout和RelativeLayout使用详解
- Android中RelativeLayout各个属性的含义
- Android中RelativeLayout各个属性的含义
- Android中RelativeLayout各个属性的含义
- Android中RelativeLayout各个属性的含义
- Android中RelativeLayout各个属性的含义
- Android中RelativeLayout各个属性的含义
- 蓝桥杯-基础练习之回文数——BASIC-8
- 思戀妳的日子裏
- 克隆的理解
- 13个最狂帅炫酷的前沿科技研究项目
- 蓝桥杯-基础练习之特殊回文数——BASIC-9
- Android中RelativeLayout的使用-----------转载下----以免忘记.
- PAT_ADVANCED LEVEL 1071 C++ SLT MAP 小记
- 不知道我們來自哪裏
- JSF中Initial Request 和 Postback Request 的区别
- Linux常用命令
- 13个最狂帅炫酷的前沿科技研究项目
- QSlider 的样式设置
- poi操作excel工具类
- linux 知识积累