The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          在LinearLayout中,如果將其定位方向設為橫向排列:android:orientation="horizontal",那么這個布局中的控件將自左向右排列。

          但有時會有這樣的情況:行的左邊有兩個控制的同時,行的右邊也有一個控制。

          如圖:

          這怎么處理呢?

          我們可以將右邊的控件放在另一個LinearLayout中,同時將其對齊方式設為右對齊:android:gravity="right",還有一點,這個LinearLayout的寬度設為充滿父控件: android:layout_width="fill_parent"。這樣就行了。

          完整的XML代碼如下:

          Xml代碼  收藏代碼
          1. <?xml version="1.0" encoding="utf-8"?>  
          2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
          3.     android:layout_width="fill_parent"  
          4.     android:layout_height="fill_parent"  
          5.     android:background="@drawable/bg"  
          6.     android:orientation="horizontal" >  
          7.   
          8.     <TextView  
          9.         android:layout_width="wrap_content"  
          10.         android:layout_height="wrap_content"  
          11.         android:text="左邊1" />  
          12.   
          13.     <TextView  
          14.         android:layout_width="wrap_content"  
          15.         android:layout_height="wrap_content"  
          16.         android:text="左邊2" />  
          17.     <!-- 將TextView包在另一個LinearLayout中  
          18.          注意android:layout_width和android:gravity這兩個屬性  
          19.      -->  
          20.     <LinearLayout  
          21.         android:layout_width="fill_parent"  
          22.         android:layout_height="wrap_content"  
          23.         android:gravity="right" >  
          24.   
          25.         <TextView  
          26.             android:layout_width="wrap_content"  
          27.             android:layout_height="wrap_content"  
          28.             android:layout_marginRight="10dp"  
          29.             android:text="右邊" />  
          30.     </LinearLayout>  
          31.   
          32. </LinearLayout>  
          posted on 2014-12-16 15:42 Eric_jiang 閱讀(337) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 新和县| 射洪县| 临夏市| 浑源县| 额济纳旗| 河西区| 阆中市| 太仆寺旗| 许昌市| 曲沃县| 夏邑县| 常宁市| 深圳市| 定安县| 满城县| 隆尧县| 额济纳旗| 黑龙江省| 泽库县| 疏勒县| 德保县| 凭祥市| 衡山县| 曲周县| 民乐县| 南陵县| 临夏市| 绥芬河市| 毕节市| 德安县| 乌恰县| 兴安盟| 渝中区| 泽普县| 海城市| 花垣县| 镇康县| 玉门市| 正宁县| 澜沧| 米脂县|