線性布局中如何使實現組件之間有間隔

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.