LinearLayout android:layout_weight的理解

一 不正确的理解

将各个view(BUTTON等)的android:layout_weight 看作比例关系在父VIEW中显示。

二 自己的理解

weight是重要性标识,值越大重要性越强。

重要性越强,意味着占据父VIEW剩余空间的能力越强。剩余空间可能很大也可能很小,甚至没有。

三 举例说明证实自己的理解

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" >

<Button android:layout_weight="1" android:id="@+id/btn_ok" android:layout_width="wrap_content" android:text="Button1111111111111111111111111111111" android:layout_height="wrap_content"></Button>

<Button android:layout_weight="2" android:id="@+id/btn_ok2" android:layout_width="wrap_content" android:text="Button2" android:layout_height="wrap_content"></Button>

<Button android:layout_weight="3" android:id="@+id/btn_ok3" android:layout_width="wrap_content" android:text="Button3" android:layout_height="wrap_content"></Button>

</LinearLayout>

btn_ok3重要性最高,所以它占有父view剩余空间能力最强。父view会先紧着btn_ok和btn_ok2显示,这两个view的显示会按其所需要的内容空间进行显示,剩余无论多大的空间都分配给btn_ok3显示。 

   

原文地址:https://www.cnblogs.com/maoyu417/p/2110169.html