我讀一本關於Android和我困在這裏怪異的行爲:重
下面是說明:
- 使用我們最近創建的實際UI項目,但讓我們從完整的 佈局開始。右鍵單擊項目 explorer中的佈局文件夾。在彈出的上下文敏感選項菜單中,選擇新建| 佈局資源文件。
- 確保爲根元素選擇了LinearLayout。
- 將文件命名爲list_detail_layout,然後單擊確定。
- 在屬性窗口中,找到默認提供的LinearLayout的方向屬性,並將其更改爲水平。
- 將LinearLayout(垂直)拖放到設計上。
- 現在拖動一個的LinearLayout(水平)到設計
- 根的LinearLayout內選擇第一個(垂直)的LinearLayout,發現 其佈局:重量屬性,並將其它的背景設置爲40設定爲 一個顏色您可以通過查找並左鍵單擊背景屬性省略號......, 然後左鍵單擊「顏色」選項卡並選擇一種顏色。
- 在根LinearLayout中選擇第二個(水平)LinearLayout, 找到它的佈局:weight屬性,並將其設置爲60.我們現在有兩個 屏幕清晰可辨的區域:一個佔40%,另外60個%, 如下圖所示:
Screen Shot 我遵循所有的步驟,但我仍然沒有得到圖像的結果。這是我的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」它的工作原理,但我不知道爲什麼沒有在書中提到...
你需要分配權重總和。如果權重總和= 1,那麼它的40%是.40 –
您沒有在父級佈局上給出weight_sum –