LinearLayout(线性布局)是Android六大布局之一,屏幕适配用的比较多的就是LinearLayout的weight(权重属性)
1. 简单用法归纳:
按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;类推,竖直方向,只需设android:height为0dp,然后设weight属性即可。
2. 直接用wrap_content
wrap_content比较简单,对android:layout_weight赋值,直接就按比例。
3. match_parent需要计算
例如:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
我们可以发现,最终显示比例是2:1,而不是1:2:3
这里其实没那么简单的,还是需要我们计算的
step1: 个个都是fill_parent,但是屏幕只有一个,那么1- 3 = - 2 fill_parent
step2: 依次比例是1/6,2/6,3/6
step3: 先到先得,先分给one,计算:1 - 2 * (1/6) = 2/3 fill_parent 接着到two,计算:1 - 2 * (2/6) = 1/3 fill_parent 最后到three计算1- 2 * (3/6) = 0 fill_parent
step4:所以最后的结果是:one占了两份,two占了一份,three什么都没有