android多设备界面适配的利器:属性weight的妙用

1、按比例显示控件元素

    <EditText
        android:id="@+id/edit_message"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" >
        <requestFocus />
    </EditText>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_send" />


属性weight的作用(不翻了,将就看吧):

if you give one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view fills 2/3 of the remaining space and the second view fills the rest. If you add a third view and give it a weight of 1, then the first view (with weight of 2) now gets 1/2 the remaining space, while the remaining two each get 1/4.

EditText 占 2/3  ,  Button 占 1/3. 

注意:只有当这两个控件的layout_width、layout_height都设置为"wrap_content"时,这个规则才能够成立。

2、一个控件独占,其它控件根据自身内容自动调整。

将需要独占的控件,其weight设置为1,同时宽度设置为0dp。根据自身内容调整的控件,不要设置weight。

效果:

原文地址:https://www.cnblogs.com/ygm900/p/3521328.html