LinearLayout之weight属性

    xiaoxiao2023-10-18  172

    1.android:layout-weight 这个属性允许我们使用比例的方式来指定控件的大小,它在手机屏幕的适应性方面可以起到一个非常重要的作用。 将android:layout-weight属性的值设置为1就会平分屏幕宽度。原理如下,系统会先将所有layout下所有控件指定的:layout-weight值相加,最后再平均分配 此种方式适用于水平方向的控件布局

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

    <EditText android:id="@+id/input_message" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="type something" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/send" android:text="send"/>

    2.如果只是设置其中一个的 android:layout_weight的值为1,剩下另一个设置为适应内容大小,那么设置的将占据剩余屏幕宽度,例子如下:

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

    <EditText android:id="@+id/input_message" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="type something" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/send" android:text="send"/>
    最新回复(0)