比如有如下一个布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.weight.MainActivity" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111" /> <TextView android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2" /> <TextView android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3" /> </LinearLayout>
文字的底端是对齐的
想要得到预期的效果
需要加入:
android:baselineAligned="false"
如果把第一个TextView的layout_weight属性设置为wrap_content
android:layout_width="wrap_content"
说明,LinearLayout中的layout_weight属性,首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配。
假设有如下布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.weight.MainActivity" android:baselineAligned="false" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111" /> <TextView android:layout_width="match_parent" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2" /> <TextView android:layout_width="match_parent" android:layout_height="48dp" android:layout_weight="2" android:background="#440000ff" android:gravity="center" android:text="3" /> </LinearLayout>
参考:首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配
那么剩余的尺寸是:rest = 480 - 480 * 3 = - 480 * 2
TextView 1 : 480 + ( - 480 * 2 / 5) = 480 * (3 / 5)
TextView 2, 3 : 480 + ( - 480 * 2 ) * ( 2 / 5 ) = 480 * (1 / 5 )
效果如下图所示:
(剩余的尺寸可以是负值)
控件宽度 + 父控件剩余宽度 * 比例
实现只有一个TextView且只占用屏幕的一半:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="2" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111" /> </LinearLayout>
效果如下: