【问题标题】:TextView is Not Showing any TextTextView 不显示任何文本
【发布时间】:2018-08-29 12:58:03
【问题描述】:

我刚刚安装了 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

标签: android android-layout android-studio


【解决方案1】:

试试你的约束布局。

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

【讨论】:

    【解决方案2】:

    可能是一个迟到的答案,但只要它对某人有帮助。

    您正在使用属性tools:text 作为最后一个文本视图。 tools 命名空间仅用于 android studio 中的布局预览。

    要让 textview 在运行时显示某些内容,您必须使用 android 命名空间和 android:text 属性,如下所示:

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

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

    而且,如果您的设计窗口没有显示任何文本而是显示空白屏幕,您可能需要更改设计窗口中的“预览主题”以匹配您的应用主题(例如:Material 主题、Light、AppTheme 等。 ) The sixth button, which shows "Material Theme" right now

    【讨论】:

      【解决方案3】:

      您正在使用 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 除了空白屏幕
      • @fiverrfreelancer 尝试运行它,有时设计未正确构建,但当您运行它时它已修复
      【解决方案4】:

      如果你要使用约束布局,你需要约束你的小部件。 首先尝试使用线性布局,如果您只想显示带有 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,除了空白屏幕
      • 所以你的android ide安装可能有问题,因为linearlayout背景不是白色
      【解决方案5】:

      试试这个

      <?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,除了空白屏幕。
      • 对,这正是不该做的。不要使用工具命名空间。您将需要使用 android 命名空间。如果不注意,很容易掩盖。
      【解决方案6】:

      用户 android:text="你好世界!" 而不是 tool:text="hello world"

      【讨论】:

      • 这已经在其他答案中提到了。 在回答已有答案的旧问题时,请确保提供新颖的解决方案或比现有答案更好的解释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多