布局管理器
线性布局(LinearLayout)
常用属性
android:id = "@+id/user"
android:background = "@mipmap/bg" // 图片
android:background = "#FF00FF" // 单色
android:layout_width //宽度,wrap_content 包含内容;match_parent 匹配父空间
android:layout_height //高度
android:layout_margin //外边距
android:layout_marginTop //外边距顶部
android:layout_marginBottom //外边距底部
android:layout_marginLeft
android:layout_marginRight
android:padding //内边距,四个方向一样
android:paddingLeft // 不同方向边距
android:paddingTop
android:paddingRight
android:paddingBottom
android:orientation //该属性不设置时默认为horizontal(水平),vertical垂直方向
android:gravity //内部元素排列的对齐方式
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="#7373B9"
android:padding="25dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1E9E9"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#FF5809"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFFFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#CA8EFF"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="10dp">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ACD6FF"
android:layout_weight="1"
/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FFE66F"
android:layout_weight="2"
/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FFC1E0"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
效果:
相对布局(RelativeLayout)
相对布局特有的属性:
android:layout_toLeftOf //在谁的左边
android:layout_toRightOf //右边
android:layout_alignBottom //
android:layout_below //下面
android:layout_alignParentBottom //和父空间底部对齐(左下角)
测试代码:
<?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="#9393FF"
/>
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFF93"
android:layout_below="@id/view_1"
/>
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/view_2"
android:background="#81C0C0"
android:orientation="horizontal"
android:padding="15dp"
>
<View
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#DFFFDF"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFBD9D"
android:padding="15dp"
>
<View
android:id="@+id/view_3"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#ACD6FF"
/>
<View
android:id="@+id/view_4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FFE66F"
android:layout_toRightOf="@id/view_3"
android:layout_marginLeft="15dp"
/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
效果: