1.线性布局LinearLayout
常用属性:
android:id #设置名称
android:layout_width //设置宽度,一般可取值为“match_parent”或者“xxxdp"
android:layout_height //设置高度,一般可取值为“match_parent”或者“xxxdp"
android:orientation #设置布局方式,水平布局为:“horizontal”;垂直布局为:“vertical”
android:background //设置背景颜色,例如黑色为”#000000“,白色为”#ffffff"
android:padding //设置控件内容与控件边的距离
android:layout_margin //本元素离上下左右间的距离
android:layout_weight #设置权重,此处设置的权重是剩余部分的权重,如果两个权重值相同,则相当于对界面进行平分
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id='@+id/ll_1'
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="50dp"
android:paddingBottom="10dp"
android:layout_marginBottom="20dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0033" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#0066FF"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<View
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FF0033"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#55AA99"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
运行结果为:
2.相对布局RelativeLayout
常用属性:
android:layout_toLeftOf //在谁左边
android:layout_toRightOf //在谁右边
android:layout_alignBottom //跟谁底部对齐
android:layout_alignParentBottom //跟父部底部对齐
android:layout_below //在谁下边
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000" />
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/view_1"
android:background="#FF0033" />
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/view_2"
android:background="#0066FF"
android:orientation="horizontal"
android:padding="15dp">
<View
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#ff0033" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:padding="15dp">
<View
android:id="@+id/view_3"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF9900"
/>
<View
android:id="@+id/view_4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#dd5544"
android:layout_toRightOf="@id/view_3"
android:layout_marginLeft="10dp"
/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
运行结果: