TextView未显示

7
我有一个线性布局和一个文本视图在另一个线性布局内。里面的线性布局被显示出来了,但问题是最后一个文本视图没有显示。为什么?
    <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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="Scrie un cuvant" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/cuvant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/cuvant_hint"
            android:inputType="text" />

        <Button
            android:id="@+id/cuvantbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cuvantbutton_text" />
    </LinearLayout>


        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="Sau random" />
</LinearLayout>

看这里,

输入图像描述


1
你的LinearLayout因为高度设置为match_parent,所以覆盖了最后一个textView。你可以使用其他解决方案,比如layout_weight或者使用RelativeLayout。 - Opiatefuchs
5个回答

13

您的LinearLayout有一个高度属性为fill_parent。请更改为wrap_content


3

您实际上是告诉 LinearLayout 占用所有可用空间,这导致最后一个 textview 未显示。相反,请使用 "wrap_content",它会占用显示可用内容所需的空间。

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="Scrie un cuvant" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/cuvant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/cuvant_hint"
            android:inputType="text" />

        <Button
            android:id="@+id/cuvantbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cuvantbutton_text" />
    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="Sau random" />

</LinearLayout>

2

将内部 LinearLayoutlayout_heightmatch_parent 改为 wrap_content


1

你的内部布局高度设置为match_parent,因此导致最后一个文本视图不可见。

<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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="Scrie un cuvant" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   <<---- Change it to wrap_Content from match_parent
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/cuvant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/cuvant_hint"
            android:inputType="text" />

        <Button
            android:id="@+id/cuvantbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cuvantbutton_text" />
    </LinearLayout>


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="Sau random" />
</LinearLayout>

0
父布局必须覆盖文本视图,以便在其中获取文本。
存在问题的布局,无法显示文本-----------
<LinearLayout
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/Crime">

        </ImageView>

        <LinearLayout
            android:layout_width="330dp"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="God Father"
                android:textSize="30dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Crime Drama"
                android:textSize="30dp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>

在更改覆盖TextView的父布局大小后。

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/Crime">

        </ImageView>

        <LinearLayout
            android:layout_width="330dp"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="God Father"
                android:textSize="30dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Crime Drama"
                android:textSize="30dp"
                android:textStyle="bold" />
        </LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接