目录
特此说明,本篇文章针对基本熟悉相关属性的前提下进行
weight属性解析:
实例一(wrap_content使用情形):
activity_main.xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- fill_parent equals to match_parent -->
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/three"
android:background="#FF00FF"
/>
</LinearLayout>
结果如下:
当要进行垂直划分时,需要将父布局orientation属性设置为horizontal
实例二(match_parent使用情形):
activity_main.xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- fill_parent equals to match_parent -->
<TextView
android:id="@+id/textview1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/one"
android:background="#98FB98"
/>
<LinearLayout
android:id="@+id/LinearLayoutChild"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textview2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/two"
android:background="#FFFF00"
/>
<TextView
android:id="@+id/textview3"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/three"
android:background="#FF00FF"
/>
<TextView
android:id="@+id/textview4"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/four"
android:background="#768732"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/five"
android:background="#D85C86"
/>
</LinearLayout>
结果如下:
当要进行垂直划分时,需要将父布局orientation属性设置为horizontal
这种情形下的各部分比例划分有一种特别的计算方法:
由于父布局中含有三个部分,分别是textview1、LinearLayoutChild、textview5,并且它们的layout_height属性均是match_parent,但是整体布局仅能存在一个match_parent。于是 1 - 3 = -2,textview1 = 1 - 2(1/3) = 1/3,LinearLayoutChild = 1 - 2(1/3) = 1/3,textview5 = 1 - 2(1/3) = 1/3。同样在LinearLayoutChild中的元素也采用此计算方法进行比例划分,依次是textview2 = 1 - 2(1/6) = 2/3,textview3 = 1 - 2(2/6) = 1/3,textview4 = 1 - 2(3/6) = 0。整个计算结果就代表着各个元素在整个布局中的所在比例。
LinearLayout设置分割线
实例一(添加一个View):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_gravity="center"
android:background="#000000"/>
</LinearLayout>
结果如下: