在LinearLayout中,可以使用android:layout_weight屬性來指定子元素所占的權(quán)重。該屬性值是一個(gè)浮點(diǎn)數(shù),用于指定子元素在父布局中所占的比例。
例如,如果一個(gè)LinearLayout中有兩個(gè)子元素,一個(gè)設(shè)置了android:layout_weight=“1”,另一個(gè)設(shè)置了android:layout_weight=“2”,則第一個(gè)子元素占總空間的1/3,而第二個(gè)子元素占總空間的2/3。
示例代碼如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Item 1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Item 2" />
</LinearLayout>
在上面的示例中,第一個(gè)TextView的權(quán)重為1,第二個(gè)TextView的權(quán)重為2,因此第一個(gè)TextView占總空間的1/3,第二個(gè)TextView占總空間的2/3。