当前位置: 首页 > 知识库问答 >
问题:

TextView在AndroidStudio中显示,但不在设备上显示[重复]

常小白
2023-03-14

我知道以前有人问过这个问题,但那篇文章中的解决方案对我不起作用。

我刚刚开始学习Android编程,当时正在做一个模型。它可以在Android Studio中正确显示,但不能在我的设备上显示。图像和背景色显示正确,但没有文本显示。我试过改变字体和字体颜色。需要改变什么?

我有另一个使用RelativeLayout的示例,它显示正确,但这个ConstraintLayout不正确。

第一个图像来自AS,另一个来自我的设备。

这是活动_main中的代码

<?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:id="@+id/listLayout"
    android:layout_width="wrap_content"
    android:layout_height="248dp"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="4dp"
    android:layout_marginTop="4dp"
    tools:context="com.vijer.test1.MainActivity">
    <!-- First Group -->
    <View
        android:id="@+id/group_one"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintLeft_creator="@id/listLayout"
        app:layout_constraintTop_creator="@id/listLayout" />

    <ImageView
        android:id="@+id/album_art1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/image_description"
        android:src="@drawable/image1"
        app:layout_constraintLeft_creator="@id/listLayout"
        app:layout_constraintTop_toTopOf="@id/listLayout" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/meta_artist1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/album_art1"
        app:layout_constraintTop_toTopOf="@id/listLayout"
        tools:text="@string/tag_artist1" />

    <TextView
        android:id="@+id/meta_title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/album_art1"
        app:layout_constraintTop_toBottomOf="@id/meta_artist1"
        tools:text="@string/tag_title1" />

    <TextView
        android:id="@+id/meta_album1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@id/album_art1"
        app:layout_constraintTop_toBottomOf="@id/meta_title1"
        tools:text="@string/tag_album1" />

    <TextView
        android:id="@+id/meta_time1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/meta_delimiter1"
        app:layout_constraintTop_toBottomOf="@id/meta_title1"
        tools:text="@string/tag_length1" />

    <TextView
        android:id="@+id/meta_delimiter1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/meta_file_type1"
        app:layout_constraintTop_toBottomOf="@id/meta_title1"
        tools:text="@string/tag_delimiter" />

    <TextView
        android:id="@+id/meta_file_type1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginTop="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="@+id/meta_time1"
        app:layout_constraintEnd_toEndOf="@+id/listLayout"
        tools:text="@string/tag_file_type1" />

    <!-- Second Group -->

    <View
        android:id="@+id/group_two"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="4dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintLeft_toLeftOf="@id/listLayout"
        app:layout_constraintTop_toBottomOf="@id/group_one"
        tools:layout_editor_absoluteX="0dp" />

    <ImageView
        android:id="@+id/album_art2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginTop="4dp"
        android:contentDescription="@string/image_description"
        android:src="@drawable/image2"
        app:layout_constraintLeft_toLeftOf="@id/listLayout"
        app:layout_constraintTop_toBottomOf="@id/group_one" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/meta_artist12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/album_art2"
        app:layout_constraintTop_toBottomOf="@id/group_one"
        tools:text="@string/tag_artist2" />

    <TextView
        android:id="@+id/meta_title12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="30dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/album_art2"
        app:layout_constraintTop_toBottomOf="@id/group_one"
        tools:text="@string/tag_title2" />

    <TextView
        android:id="@+id/meta_album12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@id/album_art2"
        app:layout_constraintTop_toBottomOf="@id/group_one"
        tools:text="@string/tag_album2" />

    <TextView
        android:id="@+id/meta_time12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/meta_delimiter12"
        app:layout_constraintTop_toBottomOf="@id/group_one"
        tools:text="@string/tag_length2" />

    <TextView
        android:id="@+id/meta_delimiter12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/meta_file_type12"
        app:layout_constraintTop_toBottomOf="@id/group_one"
        tools:text="@string/tag_delimiter" />

    <TextView
        android:id="@+id/meta_file_type12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"

        app:layout_constraintBottom_toBottomOf="@+id/meta_time12"
        app:layout_constraintEnd_toEndOf="@+id/group_one"
        tools:text="@string/tag_file_type2" />

    <!-- Third Group -->
    <View
        android:id="@+id/group_three"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="4dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintLeft_toLeftOf="@id/listLayout"
        app:layout_constraintTop_toBottomOf="@id/group_two" />

    <ImageView
        android:id="@+id/album_art3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginTop="4dp"
        android:contentDescription="@string/image_description"
        android:src="@drawable/image3"
        app:layout_constraintLeft_toLeftOf="@id/listLayout"
        app:layout_constraintTop_toBottomOf="@id/group_two" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/meta_artist13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="4dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/album_art3"
        app:layout_constraintTop_toBottomOf="@id/group_two"
        tools:text="@string/tag_artist3" />

    <TextView
        android:id="@+id/meta_title13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="30dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/album_art3"
        app:layout_constraintTop_toBottomOf="@id/group_two"
        tools:text="@string/tag_title3" />

    <TextView
        android:id="@+id/meta_album13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@id/album_art3"
        app:layout_constraintTop_toBottomOf="@id/group_two"
        tools:text="@string/tag_album3" />

    <TextView
        android:id="@+id/meta_time13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/meta_delimiter13"
        app:layout_constraintTop_toBottomOf="@id/group_two"
        tools:text="@string/tag_length3" />

    <TextView
        android:id="@+id/meta_delimiter13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/meta_file_type13"
        app:layout_constraintTop_toBottomOf="@id/group_two"
        tools:text="@string/tag_delimiter" />

    <TextView
        android:id="@+id/meta_file_type13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginTop="56dp"
        android:fontFamily="@font/abeezee"
        android:textColor="#ffffff"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="@+id/meta_time13"
        app:layout_constraintEnd_toEndOf="@+id/group_two"
        tools:text="@string/tag_file_type3" />

</android.support.constraint.ConstraintLayout>

共有2个答案

南门志
2023-03-14

你已经使用了文本视图与工具:文本属性,这将显示文本只在你的Android Studio设计,但不是在设备上。

我建议你保持这样的变化。不要更改xml中的任何内容,唯一需要做的就是从代码中设置这些文本视图字符串。

阅读下面关于工具及其用法的文章。https://android.jlelse.eu/tools-attributes-hidden-gems-of-android-studio-d7451b194e0b

丁理
2023-03-14

使用

android:text=“@string/tag\u artist1”

而不是

工具:text=“@string/tag\u artist1”

在所有文本视图中都相同。工具仅用于在开发人员模式下显示文本。这有助于在创建xml时测试UI元素

 类似资料: