![](/img/trans.png)
[英]Android: Nested Linearlayouts will not display a grid of buttons, Matching parent
[英]Android: nested linearlayouts error
作為測試,我試圖在屏幕的上半部分創建三行圖像(以垂直方向均勻間隔)。 由於某種原因,android:layout_height =“ 0dp”給我一個錯誤,說這將使視圖不可見。 沒錯,我看不到任何東西。 為什么會這樣? 我將權重更改為1,為什么它不擴展以占用可用空間? 謝謝!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/arches_utah"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/blue_sea_mountains"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/china_jungle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
元素上的layout_weight
適用於該元素的父元素。 既然你父元素具有水平方向,你只能使用layout_weight
與配對layout_width
。 如果您的根LinearLayout
是vertical
則可以將layout_height
設置為0dp
而改用layout_weight
。
從您的問題來看,聽起來您應該在第一個子級LinearLayout
的三個元素上設置權重和高度。
必須在與父對象的方向相同的軸上指定weight屬性。 由於您的父LinearLayout
具有水平方向,因此weight
屬性在寬度上起作用,因此您需要在子LinearLayout
上使用layout_height="wrap_content"
和layout_width="0dp"
。
所以問題出在兩個LinearLayouts
之間。
您尚未關閉父級線性布局,布局寬度與重量而不是高度相關...您也可以像這樣使用weightSum
...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/arches_utah" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/blue_sea_mountains" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/china_jungle" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.