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:orientation="horizontal" tools:context="com.example.listviewrefreshupdata.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:background="#44ff0000" android:text="111111111111111111111111111"/> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_weight="2" android:background="#4400ff00" android:text="22222"/> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="3" android:gravity="center" android:background="#440000ff" android:text="3333333333"/> </LinearLayout>显示效果为:
例二:
将三个TextView的宽度都设置为match_parent
<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:orientation="horizontal" tools:context="com.example.listviewrefreshupdata.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:background="#44ff0000" android:text="11"/> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:layout_weight="2" android:background="#4400ff00" android:text="2"/> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="2" android:gravity="center" android:background="#440000ff" android:text="2"/> </LinearLayout>显示效果为:
理由:
例三:
如果要实现如下效果:
那么可以设置父布局的
android:weightSum属性为2,然后设置textView的layout_weight为1
即:
<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:orientation="horizontal" android:weightSum="2" tools:context="com.example.listviewrefreshupdata.MainActivity"> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:background="#44ff0000" android:text="111111111111"/> </LinearLayout>
例四:
<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:orientation="horizontal" tools:context="com.example.listviewrefreshupdata.MainActivity"> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:background="#44ff0000" android:text="111111111111"/> <TextView android:layout_width="0dp" android:layout_height="50dp" android:gravity="center" android:layout_weight="2" android:background="#4400ff00" android:text="2"/> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="3" android:gravity="center" android:background="#440000ff" android:text="2"/> </LinearLayout>预期效果应该是:
实际效果是:
为了达到预期效果,应该设置父布局的
android:baselineAligned="false"
即:
这样就可以达到预期效果了
总结:凡是以 "layout_"开头的属性都是由父控件去设置的属性,eg :layout_gravity:子控件相对父控件,gravity:子控件自身内容的相对位置