线性布局中如何使实现组件之间有间隔

在研究android的Contacts源码的时候,遇到了一个布局文件,是用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="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="scnu"
        android:textColor="?attr/call_log_primary_text_color"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/network_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:singleLine="true"
        android:text="cdma"
        android:textColor="?attr/call_log_secondary_text_color"
        android:textSize="14sp" />

</LinearLayout>


1. android:layout_weight 的使用

    所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。若赋一个大于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值,以及该值在整个视图屏幕布局的layout_weight值中所占的比率而决定。 数值越大,占用的空间越大。

2. android:gravity="right",只有在上面设置了layout_weight,将各控件分配了具体的大小,这个属性才体现出效果。

    当 android:orientation="vertical" 时,只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
    当 android:orientation="horizontal" 时,只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值