简体   繁体   English

片段中的 Textview 显示背景颜色但不显示文本

[英]Textview in Fragment Showing background color but not text

I have a piece of code that relies on android-fragments but I am facing an issue when the text in the textview does not show when I run the app in my phone while on android studio, it shows perfectly.我有一段依赖于 android-fragments 的代码,但是当我在 android studio 上运行应用程序时,当 textview 中的文本没有显示时,我遇到了一个问题,它显示完美。 My code is as follows:我的代码如下:

MainActivity.java MainActivity.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    openFragment(HomeFragment.newInstance("", ""));
}

public void openFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<androidx.drawerlayout.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

  <include
     layout="@layout/app_bar_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/includeTool"/>

  <FrameLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/container"/>

  <com.google.android.material.navigation.NavigationView
     android:id="@+id/nav_view"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     android:fitsSystemWindows="true"
     app:headerLayout="@layout/nav_header_main"
     app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

fragment_home.xml fragment_home.xml

<?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=".fragments.HomeFragment">

   <View
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="@color/bg_screen1"
      android:layout_alignParentTop="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentLeft="true"
      android:id="@+id/view_container_top"
      android:visibility="invisible"/>

   <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@id/view_container_top"
      android:layout_above="@id/view_container_bottom">

     <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/pic"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:contentDescription="TODO" />

     <TextView
        android:id="@+id/text_home"
        android:layout_below="@id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="308dp"
        android:layout_marginEnd="8dp"
        android:gravity="center_horizontal"
        android:textAlignment="center"
        android:textSize="20sp"
        tools:text="home fragment" />
   </RelativeLayout>
   <View
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:visibility="invisible"
      android:id="@+id/view_container_bottom"
      android:layout_alignParentBottom="true" />
</RelativeLayout>

Also below is a screenshot of how the app appears on the phone.下面也是该应用程序在手机上的显示屏幕截图。 As you can see from the screenshot the purple horizontal line in the middle of the screen is the textview background with no text on it.从屏幕截图中可以看出,屏幕中间的紫色水平线是 textview 背景,上面没有文字。

截屏

Change:改变:

tools:text="home fragment" 

To:至:

android:text"home fragment"

The tools tag is for Android Studio, the device won't see it tools标签用于 Android Studio,设备看不到

The problem is in TextView tag, replace the tools:text to android:text here:问题出在 TextView 标签中,将 tools:text 替换为 android:text here:

<TextView
    android:id="@+id/text_home"
    android:layout_below="@id/pic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="308dp"
    android:layout_marginEnd="8dp"
    android:gravity="center_horizontal"
    android:textAlignment="center"
    android:textSize="20sp"
    android:text="home fragment" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM