繁体   English   中英

Android:嵌套的linearlayouts错误

[英]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 如果您的根LinearLayoutvertical则可以将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM