RelativeLayout相对布局(以下方法仅在RelativeLayout标签中起作用)
一种相对于页面坐标进行布局的方式:一般将整个屏幕定义为父布局。
在父布局中放置元素的方法有(align:一种设置元素对齐的方式):
android:layout_alignParentLeft="true";//在父布局左侧
android:layout_alignParentRight="true";//在父布局右侧
android:layout_algnParentTop="true";//在父布局上方
android:layout_alignParentBottom="true";//在父布局下方
元素间相对位置的确定(一般会结合其他定位属性一起确定元素的位置)
android:layout_toRightOf="@id/button1";//在控件button1的右边,不仅仅是紧靠着
android:layout_toLeftOf="@id/button1";//在控件button1的左边,不仅仅是紧靠着
android:layout_below="@id/button1";//在控件button1下面,不仅仅是正下方
android:layout_above="@id/button1";//在控件button1下面,不仅仅是正下方
定义和某控件对齐
android:layout_alignTop="@id/button1";//和button1上对齐
android:layout_alignBottom="@id/button1";//和button1下对齐
android:layout_alignLeft="@id/button1";//和button1左对齐
android:layout_alignRight="@id/button1";//和button1右对齐
定义元素的水平居中
android:layout_centerHorizontal="true";//在父布局中水平方向居中
android:layout_centerVertical="true";//在父布局中垂直方向居中
android:layout_centerInParent="true";//将此元素置于父布局的中心位置
在代码中应用这些属性的时候,用如下方式的代码实现:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
LinearLayout线性布局
线性布局也是一种比较灵活的布局方式, 通过直接的线性布局对页面直接实现布局。在使用LinearLayout 的时候,思路大致是将页面母板分成若干部分,然后母板LinearLayout 使用android:orientation="vertical"将各个部分垂直分布,然后每个部分中的各个对象通过android:orientation="horizontal"实现各个对象的横向分布。
设置控件在一行或一列中所占比例值:android:layout_weight="1"(仅在LinearLayout标签中起作用)。示例:
<LinearLayout 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:orientation="vertical"
tools:context="${packageName}.${activityClass}"
tools:ignore="Orientation" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name" />
<EditText
android:id="@+id/tipUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:text="@string/tip_user_name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_password" />
<EditText
android:id="@+id/tipPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword"
android:text="@string/tip_user_password" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/logInBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_Btn" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rember_pass" />
</LinearLayout>
</LinearLayout>
大概运行结果就是这样的: