直接上代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yunlibeauty.test.MainActivity" android:orientation="vertical"
android:id="@+id/base_layout"><LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent" android:layout_height="0dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent" android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout2_1"
android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="1"
android:background="@color/colorC"
tools:ignore="Suspicious0dp" />
<LinearLayout
android:id="@+id/layout2_2"
android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="1"
android:background="@color/colorB"
tools:ignore="Suspicious0dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorPrimary" >
</LinearLayout>
</LinearLayout>
我这里首先把屏幕垂直分成了三分, layout1、layout2、layout3. 在layout2里我又水平分成两份 layout2_1及layout2_2
结合代码LinearLayout常用属性:
1:id作为layout的标识
2:orientation布局的方向,是垂直布局还是线性布局,比如最外层id为base_layout的我设为了垂直,先垂直分成的三分。而在layout2里我设置了水平布局,所以水平分成了两份
3:layout_width、layout_height,指定的是布局的宽高,一般不直接去写数字,会设为fill_parent\match_parent(这里我用了或,因为他们两是一样的,Android2.2以后是一样的,2.2之前没有match_parent)、wrap_content组件的实际大小
4:layout_weight 占的权重,比如我都设为了1,如果我将id为layout1的设为2,那layout1将会占用屏幕的一半。
5:backgroud:背景,可以设置为颜色或者图片