【问题标题】:Nested layout for android app is not workingandroid应用程序的嵌套布局不起作用
【发布时间】:2016-12-18 07:41:47
【问题描述】:

这是我创建的 XML 文件。我无法设置LinearLayout 的高度。我已将LinearLayout 的方向设置为水平,并使用layout_weight 设置LinearLayout 的高度。

但是,当我将 TextView 的高度设置为 match_parent 时,它不起作用。

<RelativeLayout 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:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".MainActivity"
    android:weightSum="3">

    <LinearLayout 
        android:id="@+id/a"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/b"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_below="@id/a">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/c"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_below="@id/b">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="5"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6"/>
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

  • “不工作”和“不工作”是什么意思?

标签: android android-layout android-view android-xml


【解决方案1】:

它不起作用,因为RelativeLayout(您的根布局)不支持权重。因此,使用vertical orientation 将您的顶级RelativeLayout 更改为LinearLayout,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多