线性布局(LinearLayout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" //宽度
android:layout_height="match_parent" //高度
android:background="#123456" //背景颜色
android:orientation="vertical" //对齐方式(垂直方向)>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" //第一个占父容器的权重为1/2
android:background="#abcdef"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" //占第二个父容器的权重为1/2
android:background="#aaaabb" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" //占第二个父容器的权重为1/2
android:background="#00bbee" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" //占第一个父容器的权重的1/2
android:background="#0000aa"
android:weightSum="2" //分配的权重为2
android:gravity="center">
<LinearLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1" //占父容器中的2份权重的1份!
</LinearLayout>
</LinearLayout>
</LinearLayout>
布局示例
android:background=”#ddeeff” >
相对布局(RelativeLayout)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaaa00" >
<RelativeLayout //相对与父容器
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/center"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout //相对于兄弟容器
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_above="@id/center"
android:layout_toLeftOf="@id/center"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignTop="@id/center"
android:layout_toLeftOf="@id/center"
android:background="#123456" >
</RelativeLayout>
<RelativeLayout
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/center"
android:layout_toLeftOf="@id/center"
android:background="#123456" >
</RelativeLayout>
</RelativeLayout>
本文详细介绍了线性布局、相对布局、帧布局等UI布局方式,并通过具体示例展示了不同布局的特点及应用。
2499

被折叠的 条评论
为什么被折叠?



