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

在研究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 是生效的。

你可能感兴趣的:(线性布局中如何使实现组件之间有间隔)