【问题标题】:Android: nested linearlayouts errorAndroid:嵌套线性布局错误
【发布时间】:2015-12-27 05:35:00
【问题描述】:

作为测试,我正在尝试在屏幕的上半部分创建一行三张图像(垂直方向均匀分布)。出于某种原因, 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>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    元素上的layout_weight 应用于该元素的父元素。由于您的父元素具有水平方向,因此您只能将layout_weightlayout_width 配对使用。如果您的根LinearLayoutvertical,那么您可以将layout_height 设置为0dp 并改用layout_weight

    从你的问题来看,听起来你应该在第一个孩子LinearLayout 中的三个元素上设置权重和高度。

    【讨论】:

      【解决方案2】:

      weight 属性需要指定在与 parent 的方向相同的轴上。由于您的父级LinearLayout 具有水平方向,因此weight 属性适用于宽度,因此您需要在子级LinearLayout 上使用layout_height="wrap_content"layout_width="0dp"

      所以问题出在你的 2 个LinearLayouts 之间。

      【讨论】:

        【解决方案3】:

        你还没有关闭你的父线性布局和布局宽度与重量而不是高度有关...您也可以像这样使用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>
        

        【讨论】:

          猜你喜欢
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-02
          • 1970-01-01
          • 1970-01-01
          • 2017-09-27
          相关资源
          最近更新 更多