![](/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.