嵌套的LinearLayout仅显示第一个View

时间:2013-01-10 08:30:13

标签: java android xml layout

我看到一些问题可能与此类似。但是,这些情况有所不同,有些不适合初学者 所以我最近刚开始使用Android。请理解我是*.xml的新手。无论如何,我关心的是嵌套的LinearLayout仅显示第一个View

<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="vertical"
    tools:context=".MainActivity" >

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <TextView
            android:layout_gravity="center"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:textSize="20sp"
            android:text="@string/app_title">
        </TextView>

    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <Button 
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/button_ok"
            android:onClick="changeMessage">
        </Button>

        <TextView
            android:layout_gravity="center"
            android:textSize="20sp"
            android:id="@+id/this_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/sample_text">
        </TextView>

    </LinearLayout>

</LinearLayout>

到目前为止,这是XML。使用此代码,只显示第一个TextView。

3 个答案:

答案 0 :(得分:2)

android:layout_height="match_parent"嵌套LinearLayout的高度应为wrap_content而不是match_parent

答案 1 :(得分:1)

因为您已将LinearLayout宽度和高度设为matchparent

将其高度更改为wrapcontent

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:textSize="20sp"
        android:text="@string/app_title">
    </TextView>

</LinearLayout>

// other layouts

答案 2 :(得分: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="vertical"
    tools:context=".MainActivity" >
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:textSize="20sp"
        android:text="@string/app_title">
    </TextView>

</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <Button 
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/button_ok"
        android:onClick="changeMessage">
    </Button>

    <TextView
        android:layout_gravity="center"
        android:textSize="20sp"
        android:id="@+id/this_text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/sample_text">
    </TextView>

</LinearLayout>

您的内部布局与父布局匹配,因此仅显示第一个布局。

相关问题