TextView没有显示任何文本。

6

我刚刚安装了Android Studio并创建了一个项目,但是每当我在其中添加一个TextView时,它都不显示任何东西,即使Android Studio已经创建了 TextView 也不显示。 Activity.xml 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="25dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="67dp"
        tools:text="Hello World" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        tools:text="sdfsfsfsfs" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        tools:text="Hello World" />

</android.support.constraint.ConstraintLayout>

需要帮忙的话,请告诉我。


TextView 没有约束,而您的根视图是 ConstraintLayout。请阅读有关 ConstraintLayout 的内容。 - ADM
6个回答

8

也许有点晚了,但只要能帮到某人就好。

您正在使用属性tools:text来作为最后一个textview的文本。tools命名空间仅用于在Android Studio中的布局预览。

为了在运行时显示文本视图内容,您必须使用android命名空间和android:text属性,像这样:

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

此外,只有第一个文本视图受到约束。请尝试为其余的文本视图添加约束。

如果您的设计窗口没有显示任何文本而是空白屏幕,则可能需要更改预览的主题以匹配您的应用程序主题(例如:材料主题、浅色主题、AppTheme等)。 现在显示“材料主题”的第六个按钮。


5
尝试使用约束布局来实现此功能。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Hello World" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Hello World!"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        tools:text="sdfsfsfsfs" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Hello World"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp" />

</android.support.constraint.ConstraintLayout>

此外,在应用程序运行时,id/textview不会呈现任何内容,因为没有android:text属性。 tools:text仅供布局编辑器使用。
<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        tools:text="Hello World" />

1

你正在使用ConstraintLayout,它需要提供特殊的属性才能看到所有视图。由于你说自己是初学者,我建议你从线性布局开始。请用以下代码替换你的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
 <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        tools:text="sdfsfsfsfs" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        tools:text="Hello World" />
</LinearLayout>

我尝试了,但是除了空白屏幕之外没有显示任何TextView。 - fiverr freelancer
@fiverrfreelancer 有时候尝试运行它,设计不正确,但当你运行它时,它就会被修复。 - Mohammad Rbabah

0

如果你要使用约束布局,你需要对你的小部件进行约束。首先尝试使用LinearLayout,如果你只想显示一个带有“Hello World”的TextView,那么这将更简单。

    <LinearLayout


        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#134A80">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"          
            android:text="Hello World"
            android:textStyle="bold"
            android:textColor="#FFF"
            android:layout_marginTop="8dp"/>
      </LinearLayout>

我尝试了,但是除了空白屏幕之外没有显示任何TextView。 - fiverr freelancer
你的Android IDE安装可能存在问题,因为LinearLayout背景不是白色。 - Joaquín

0

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
   >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:alignParentBottom="true"
        tools:text="Hello World" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        tools:text="sdfsfsfsfs" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Hello World" />

</RelativeLayout>

我尝试过了,但是除了空白屏幕之外没有显示任何TextView。 - fiverr freelancer
对,这正是不应该做的事情。不要使用tools命名空间。你需要使用android命名空间。如果不注意很容易忽略。 - Thomas Beauvais

-2

使用 android:text="hello world!" 而不是 tool:text="hello world"


1
这已经在其他答案中提到过了。当回答已有答案的旧问题时,请确保您提供的是新颖的解决方案或比现有答案更好的解释。 - Eric Aya

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