2017-01-15 60 views
-1

我讀一本關於Android和我困在這裏怪異的行爲:重

下面是說明:

  1. 使用我們最近創建的實際UI項目,但讓我們從完整的 佈局開始。右鍵單擊項目 explorer中的佈局文件夾。在彈出的上下文敏感選項菜單中,選擇新建| 佈局資源文件。
  2. 確保爲根元素選擇了LinearLayout。
  3. 將文件命名爲list_detail_layout,然後單擊確定。
  4. 在屬性窗口中,找到默認提供的LinearLayout的方向屬性,並將其更改爲水平。
  5. 將LinearLayout(垂直)拖放到設計上。
  6. 現在拖動一個的LinearLayout(水平)到設計
  7. 根的LinearLayout內選擇第一個(垂直)的LinearLayout,發現 其佈局:重量屬性,並將其它的背景設置爲40設定爲 一個顏色您可以通過查找並左鍵單擊背景屬性省略號......, 然後左鍵單擊「顏色」選項卡並選擇一種顏色。
  8. 在根LinearLayout中選擇第二個(水平)LinearLayout, 找到它的佈局:weight屬性,並將其設置爲60.我們現在有兩個 屏幕清晰可辨的區域:一個佔40%,另外60個%, 如下圖所示:

Screen Shot enter image description here 我遵循所有的步驟,但我仍然沒有得到圖像的結果。這是我的XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="40" 
     android:background="@android:color/holo_orange_light"></LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="60"></LinearLayout> 
</LinearLayout> 

如果我改變都layout_width值「WRAP_CONTENT」它的工作原理,但我不知道爲什麼沒有在書中提到...

+0

你需要分配權重總和。如果權重總和= 1,那麼它的40%是.40 –

+0

您沒有在父級佈局上給出weight_sum –

回答

0

android:layout_weight屬性告訴LinearLayout中如何分配它的孩子。如果您給這兩個小部件具有相同的值,但這並不一定使它們在屏幕上的寬度相同。要確定其子視圖的寬度,LinearLayout使用參數layout_widthlayout_weight的混合。

LinearLayout使兩次傳遞來設置視圖的寬度。在第一遍中,LinearLayout查看layout_width(或layout_height,垂直方向)。如果兩個LinearLayout的值爲layout_widthwrap_content,則每個視圖將只有足夠的空間來繪製自己。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="horizontal" 
android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="40" 
    android:background="@android:color/holo_orange_light"></LinearLayout> 
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="60"></LinearLayout> 
</LinearLayout> 

我猜你是初學者。我想提一提的是,編寫自己的UI設計代碼比拖放更好。

0

你必須使用weightSum在父母。

它應該看起來像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent 
    android:weightSum="100"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="40" 
     android:background="@android:color/holo_orange_light"></LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="60"></LinearLayout> 
</LinearLayout> 
+0

不起作用。第一個佈局看起來比第二個佈局要大沒有必要指定一段時間之前以及android:weightSum =「100」的行爲是相同的 pd:對不起我的英語 –

+0

嘗試更改兩個子線性佈局中的layout_width爲wrap_content。 –

+0

是的,我提到過。但這很奇怪,因爲沒有在書中提及,無論如何......已解決,謝謝大家。 –

0

當您使用安卓layout_weight確保要麼你有機器人:在父的LinearLayout weightSum設置或設置的android:layout_width = 0

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="40" 
     android:background="@android:color/holo_orange_light"> 
    </LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="60"> 
    </LinearLayout> 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="100"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="40" 
     android:background="@android:color/holo_orange_light"></LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="60"></LinearLayout> 
</LinearLayout>