TextView 在相对布局中无法工作

4
我在相对布局中遇到问题。我在我的XML布局中设置了几个textviews,但在程序运行后,它们不会在模拟器/手机上显示文字。之前我认为这是Android Studio更新版本的问题。但我已经在另一台安装了旧版Android系统的计算机系统上进行了检查。我将XML代码分享给大家。请帮帮我。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/transition_profile"
        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:orientation="vertical"
        android:background="@android:color/white">

        <ImageView
        android:id="@+id/imgv_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/round_arrow_back_ios_24_px"
        android:layout_marginTop="54dp"
        android:layout_marginLeft="28dp"
        android:layout_alignParentLeft="true"/>

        <ImageView
        android:id="@+id/imgv_help"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:src="@drawable/round_help_outline_24_px"
        android:layout_alignParentRight="true"
        android:layout_marginTop="54dp"
        android:layout_marginRight="28dp"/>

        <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imgv_boss"
        android:layout_width="126dp"
        android:layout_height="126dp"
        android:src="@drawable/boss"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="71dp"/>

        <TextView
        android:id="@+id/tv_emp_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#262d3d"
        android:letterSpacing="0.02"
        tools:text="Ramesh Singha"
        android:fontFamily="@font/roboto_slab_bold"
        android:layout_below="@id/imgv_boss"
        android:layout_centerInParent="true"
        android:layout_marginTop="26dp"/>

        <TextView
        android:id="@+id/tv_boss_profile_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#6c7383"
        android:letterSpacing="0.02"
        android:layout_below="@id/tv_emp_name"
        tools:hint="Field Manager"
        android:fontFamily="@font/roboto_slab_regular"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

        <LinearLayout
            android:id="@+id/ll_tabslides"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#fff"
            android:layout_below="@id/tv_boss_profile_name">

            <com.google.android.material.tabs.TabLayout 
             android:id="@+id/tblyt_profile" 
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="@color/white"
             android:elevation="6dp" 
             android:minHeight="?attr/actionBarSize"
             app:tabIndicatorColor="#0091ea"
             app:tabSelectedTextColor="#0091ea"
             app:tabTextColor="#6c7383"
             tools:targetApi="lollipop"/>

            <androidx.viewpager.widget.ViewPager 
             android:id="@+id/vpgr_profile"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>

        </LinearLayout>

</RelativeLayout>

也许它被您的ImageViews隐藏了。然后检查您的TextView的位置和大小。 - lambda
2
请使用 android:text 代替。 - John Joe
@lambda 不是的,所有图像都已设置为宽度和高度。还有其他问题,我不知道是什么问题。 - Saurabh Tripathi
删除所有组件,只添加TextView并检查TextView是否可见。如果TextView可见,则存在在RelativeLayout中设置组件的问题。 - Fra Red
尝试使缓存失效/重新启动。 - Ticherhaz FreePalestine
@JohnJoe 谢谢。通过使用 android:text 已经解决了。 - Saurabh Tripathi
2个回答

5

您正在使用tools:text="Ramesh Singha"textView中进行测试。如果您想设置文本,应该使用以下选项:

  1. android:text="Ramesh Singha"

  2. 在代码中添加

<TextView
     android:id="@+id/tv_emp_name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="24sp"
     android:textColor="#262d3d"
     android:letterSpacing="0.02"
     tools:text="Ramesh Singha"
     android:fontFamily="@font/roboto_slab_bold"
     android:layout_below="@id/imgv_boss"
     android:layout_centerInParent="true"
     android:layout_marginTop="26dp" />

TextView userName=(TextView)findViewById(R.id.tv_emp_name)
userName.setText("Ramesh Singha")

1

对于静态文本,请使用

android:text="Ramesh Singha"

而不是

tools:text="Ramesh Singha"

对于动态文本,请使用

.xml文件

<TextView
    android:id="@+id/tv_boss_profile_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="#6c7383"
    android:letterSpacing="0.02"
    android:layout_below="@id/tv_emp_name"
    tools:hint="Field Manager"
    android:fontFamily="@font/roboto_slab_regular"
    android:layout_centerInParent="true"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"/>

Java文件
TextView employeeName=(TextView)findViewById(R.id.tv_emp_name);
employeeName.setText("Ramesh Singha");

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