Android之xml布局中的Layout Weight是什么?

 

 

引用:

/*

Layout Weight


Equally weighted children

To create a linear layout in which each child uses the same amount of space on the screen, set theandroid:layout_height of each view to "0dp" (for a vertical layout) or theandroid:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

LinearLayout also supports assigning a weight to individual children with the android:layout_weightattribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

*/

 

weight 布局权重 在LinearLayout中,权重大的会将剩余空间完全占据。单并不会像match_parent一样覆盖掉所需内容。记住,是占据剩余空间。

在这里,我们举个权重的例子。在一个垂直分布的线性布局里,有三个TextView。以下称text1, text2 ,text3。

A.①我们把text1的权重设置为1,高设置为0dp。                               android:layout_weight = "1"

      ②text2不设权重,高宽wrap_content

      ③text3不设权重,高宽wrap_content

 

此时会变成怎么样?

text2, text3 的权重默认是0。text权重是1,那么text2与text3在整个线性布局中剩下的位置都被text1占领。

 

B.①我们把text1的权重设置为2,高设置为0dp。                               android:layout_weight = "2"(下类似)

      ②text2设权重1,高0dp

      ③text3设权重1,高0dp

此时会变成怎样?

总权重是4,text1占了一半。那么它在整个线性布局中就占一半的空间。剩下的一半空间由权重相同的text2和text3等分。

 

练习.①我们把text1的权重设置为5,高设置为0dp。                               

      ②text2设权重3,高0dp

      ③text3设权重1,高0dp

此时会变成怎么样?

请在评论区写下你的详细见解,留下大家一起来探讨!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

引用:

/*

Example


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>
 
*/转自:https://developer.android.com/guide/topics/ui/layout/linear.html#Example
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值