RelativeLayout 一些参数理解

个人理解: 相对布局:以一个对象为参照,另一个对象添加到这个对象的上,下、左、右, 左上、左下、右上、右下等位置 

 宽度包含内容  

android:layout_width

wrap_content 与内容宽度相同 

fill_parent 与父对象宽度相同

 android:layout_below="@+id/ok" 

     对象位于id为ok对象的下面 

android:layout_toLeftOf="@id/ok"  

对象位于ok对象的左边

android:layout_centerHorizontal="true" 

     水平居中,个人认为就是在容器中间对齐 

android:layout_alignLeft="@+id/ok" 

与id为ok的对象左对齐 

padding表示填充,margin表示边距 

可通过android:padding属性进行设置,4个方向的边距属性为android:paddingLeft, android:paddingRight, android:paddingTop, and android:paddingBottom. 

结论: 

*android:layout_marginBottom 

*android:layout_marginLeft 

*android:layout_marginRight 

*android:layout_marginTop 

上面几个属性的值是根据下面的相对位置的对象的值来做计算的,如果没有相对的对象就以总体布局来计算 

*android:layout_below 

*android:layout_above 

*android:layout_toLeftOf 

*android:layout_toRightOf 

*android:layout_alignTop 

*android:layout_centerHrizontal          //是否支持横屏或竖屏 

*android:layout_centerVertical             //这个根据单词的意思:中心垂直 

*android:layout_centerInparent         //  

android:layout_centerInParent="true"//居中在父对象 

android:layout_centerInParent="false" ... 浏览器不支持多窗口显示,意思就是说所有页面在单一窗口打开,这样避免了页面布局控制显示问题 

下面的相对于父的相对位置 

*android:layout_alignParentBottom 

*android:layout_alignParentLeft 

*android:layout_alignParentRight 

*android:layout_alignParentTop 

*android:layout_alignWithParentIfMissing 

实例代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/testBtn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="false"
        android:layout_marginTop="46dp"
        android:text="Button" />
        
    <Button
        android:id="@+id/activity1_btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/testBtn1"
        android:layout_below="@+id/testBtn1"
        android:layout_marginTop="20dp"
        android:text="RadioGroupStudy" />

        <Button
	        android:id="@+id/testBtn4"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@+id/activity1_btn2"
	        android:layout_centerHorizontal="true"
	        android:layout_marginTop="20dp"
	        android:text="@string/linerlayout_test" />
        
</RelativeLayout>

猜你喜欢

转载自username2.iteye.com/blog/1953717
今日推荐