在LinearLayout布局中,巧妙的設(shè)置width/height和layout_weight屬性就能達(dá)到子view平均分配父view空間的效果
例子代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_weight="1"
android:text="aaaaaaaaaaaa"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="bbbbb"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="c"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
必須要注意的是:務(wù)必將width和layout_weight搭配使用,否則代碼會(huì)報(bào)錯(cuò)
好了,上面說的是平分父view width的例子,平分父view height的方式也是一樣的,此處就不贅述了。
https://blog.csdn.net/daltsoftware/article/details/73610143