布局管理器
1.线性布局(LinearLayout)
默认水平排列(horizontal)
1.最常用属性
android :id
android :layout_width 宽度
android :layout_height 高度
android :background 背景
android :layout_margin 外边距
android :layout_padding 内边距
android :orientation 方向
android:layout_weight
就是父控件的空间大小减去组件空间的大小,然后按照权重将剩下的空间均分。(可能表达的不太对)
android:gravity
是对view控件本身来说的,是用来设置view本身的内容应该显示在view的什么位置,默认值是左侧。也可以用来设置布局中的控件位置。
android:layout_gravity
作用是声明该属性的控件,是控件本身应该放在父布局中的位置。
2.属性运用
<?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:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/day_14"
android:background="#000000"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingTop="40dp"
android:paddingLeft="20dp"
android:paddingRight="40dp"
android:layout_marginBottom="20dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E10C0C" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#2196F3"
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<!--父空间减去50dp,然后按照权重均分-->
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#DA1313"
android:layout_weight="1"/>
<!--一共5份,第一个占1/5,第二个占2/5,第三个占2/5-->
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#121010"
android:layout_weight="2"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#3DCC3D"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
3.效果图展示
2.相对布局 (RelativeLayout)
1.最常用属性
包括上面线性布局的属性,除此之外还有下面的常用属性
android:layout_toLeftOf 在某个控件的左边
android:layout_toRightOf 在某个控件的左右边
android:layout_alignBottom 在某个控件的底部
android:layout_alignParentBottom 在父控件的底部
android:layout_below 在某个控件的下面
2.属性运用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/view_1"
android:background="#000000" />
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/view_2"
android:background="#D60808"
android:layout_below="@+id/view_1" />
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/view_3"
android:background="#38B63D"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/view_1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/a11_1"
android:orientation="horizontal"
android:layout_below="@+id/view_2"
android:background="#2196F3"
android:padding="15dp">
<View
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#D12222"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:padding="15dp">
<View
android:id="@+id/view_4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#F4DD0F"/>
<View
android:id="@+id/view_5"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/view_4"
android:layout_marginLeft="15dp"
android:background="#FF5722"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
3.效果展示