TextView 不显示,即使我可以在设计和其他视图上看到它显示

TextView not showing even I can see it on design and other views showing

我有包含图像和文本的 RelativeLayout。图片显示但文字不显示,即使我可以在设计上看到它们。 我在 CoordinatorLayout.

的 include 标签中使用下面的 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toptoolbarview"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?attr/colorPrimary"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<View
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:layout_alignParentTop="true"
    android:background="@color/black" />
<ImageView
    android:id="@+id/sort"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="25dp"
    android:contentDescription="@string/provider_not_found"
    android:src="@drawable/sort" />

<TextView
    android:id="@+id/sortTxt"
    android:layout_width="32dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="15dp"
    android:layout_toEndOf="@id/sort"
    android:textColor="#FFFFFF"
    android:textSize="18sp"
    android:textStyle="bold"
    tools:text="@string/sort" />
 </RelativeLayout>

以下包含上述 xml 的地方:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
 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="match_parent"
                                             android:fitsSystemWindows="true"
                                             tools:context="io.github.luizgrp.sectionedrecyclerviewadapter.demo.HomeActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/PopupOverlay"/>

<----- 我包含 XML 的地方包含上面的代码 --- >

    <include
        layout="@layout/toptoolbar"
        android:background="?attr/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="48dp" />
</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_home"
    />

<include
    android:id="@+id/inc_bot_nav"
    layout="@layout/bottom_nav"
  />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

First Image showing the running application Second image showing the code in android design

正如您从图像中看到的那样,XML 似乎一切都应该没问题。 我的代码有什么问题? 任何帮助谢谢

您必须在 TextView

中使用 android:text="@string/sort" 而不是 tools:text="@string/sort"

根据docs

You can insert sample data in your layout preview by using the tools: prefix instead of android: with any attribute from the Android framework. This is useful when the attribute's value isn't populated until runtime but you want to see the effect beforehand, in the layout preview.

For example, if the android:text attribute value is set at runtime or you want to see the layout with a value different than the default, you can add tools:text to specify some text for the layout preview only.