剩余空间为负值时layout_weight的理解

Alt

1.剩余空间为负值时layout_weight的理解

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >
    <TextView
        android:background="#ccc"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_weight="1"
        android:textSize="30sp" />   
    <TextView
        android:background="#aaa"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_weight="2"
        android:textSize="30sp" />
     <TextView
        android:background="#999"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"    
        android:layout_weight="3"
        android:textSize="30sp" />
</LinearLayout>

Alt

当三个 TextView 的宽度都设为 wrap_content 的时候,比例是能够按照 layout_weight 分配的。原因是系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容即可,此时TextView无内容),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个TextView,所以就出现了上面的效果。

2.我们再试试,把 wrap_content 换成 fill_parent 看看(为了方便查看,我们把 layout_weight 分别改为 1 2 2)。效果就成这样了(比例3 1 1):

Alt

为什么呢?
系统先给3个 TextView 分配他们所要的宽度 fill_parent,也就是说每一都是填满他的父控件,这里就是屏幕的宽度。那么这时候的 
剩余空间=1 parent_width - 3 parent_width = -2 parent_width
(parent_width指的是屏幕宽度)
那么第一个 TextView 的实际所占宽度应该是 fill_parent 的宽度,即 parent_width + 他所占剩余空间的权重比列 1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width
同理第二个TextView的实际所占宽度为parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三个TextView的实际所占宽度为parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。
大概就是这样。
Google 官方推荐,当使用 layout_weight 属性时,可以将 width 设为 0dp,效果跟设成 wrap_content 是一样的。这样 layout_weight 就可以理解为占比了。

3.Google 官方推荐,当使用 layout_weight 属性时,可以将 width 设为 0dp,效果跟设成 wrap_content 是一样的。这样 layout_weight 就可以理解为占比了。

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >
    <TextView
        android:background="#ccc"   
        android:layout_width="0dp"   
        android:layout_height="wrap_content"   
        android:layout_weight="1"
        android:textSize="30sp" />   
    <TextView
        android:background="#aaa"   
        android:layout_width="0dp"   
        android:layout_height="wrap_content"   
        android:layout_weight="2"
        android:textSize="30sp" />
     <TextView
        android:background="#999"   
        android:layout_width="0dp"   
        android:layout_height="wrap_content"    
        android:layout_weight="3"
        android:textSize="30sp" />
</LinearLayout>

Alt

文章转自: 现代魔法学院

### 回答1: "layout_marginTop" 是一个 Android 开发的属性,用于设置视图(View)上边缘与其父容器(Parent)上边缘之间的距离,单位为像素(px),例如: ``` android:layout_marginTop="16dp" ``` 这表示将视图的上边缘与其父容器的上边缘之间的距离设置为 16dp。在布局,还有类似的属性,如:`layout_marginLeft`、`layout_marginRight`、`layout_marginBottom` 等,用于设置视图与父容器的其他边缘之间的距离。 ### 回答2: layout_marginTop 是 Android 布局的一个属性,表示控件与其上方控件或父容器之间的距离。 在 Android 的布局,通过 XML 文件来定义界面的布局结构。layout_marginTop 是其的一个属性,用于设置控件的上边距。 假设我们有一个 LinearLayout 容器,其包含了两个 TextView 控件,那么我们可以通过设置 layout_marginTop 属性来调整它们之间的间距。 例如,我们可以在第二个 TextView 控件上添加 layout_marginTop 属性,并设置一个具体的数值来控制与上方控件的距离。如果我们设置 layout_marginTop="20dp",那么第二个 TextView 将与上方的控件间距为 20dp。 如果我们将 layout_marginTop 属性设置负值,例如 layout_marginTop="-10dp",那么第二个 TextView 将会与上方控件重叠。 另外,layout_marginTop 也可以与其他的 margin 属性一起使用,例如 layout_marginLeft、layout_marginRight、layout_marginBottom,通过这些属性的组合设置,我们可以非常灵活地控制控件之间的间距和位置。 总而言之,layout_marginTop 是 Android 布局的一个重要属性,用于设置控件与其上方控件或父容器之间的距离,通过合理设置该属性,我们可以实现个性化的布局效果。 ### 回答3: layout_marginTop是Android的一个属性,表示控件与其父布局顶部的距离。它可以用来调整控件在垂直方向上的位置。 在Android,布局文件的控件需要放置在父布局,父布局可以是LinearLayout、RelativeLayout等。通过设置layout_marginTop属性,可以控制控件与父布局顶部的距离。 layout_marginTop属性接受一个数字值,表示距离的大小。正值表示控件与顶部的距离增加,负值表示距离减少。例如,如果将layout_marginTop设置为20dp,则控件与父布局顶部的距离将增加20dp。 通过合理设置layout_marginTop属性,可以实现不同控件在垂直方向上的对齐、分布等布局效果。比如,将两个控件的layout_marginTop属性设置为相同的值,可以使它们在垂直方向上对齐;将一个控件的layout_marginTop属性设置为正值,另一个控件的layout_marginTop属性设置负值,可以使它们在垂直方向上互相依赖。 总之,layout_marginTop属性是Android布局用于调整控件与父布局顶部距离的属性,通过设置它的值,可以实现各种垂直方向上的布局效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值