簡體   English   中英

片段中的 Textview 顯示背景顏色但不顯示文本

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

我有一段依賴於 android-fragments 的代碼,但是當我在 android studio 上運行應用程序時,當 textview 中的文本沒有顯示時,我遇到了一個問題,它顯示完美。 我的代碼如下:

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

<?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

<?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>

下面也是該應用程序在手機上的顯示屏幕截圖。 從屏幕截圖中可以看出,屏幕中間的紫色水平線是 textview 背景,上面沒有文字。

截屏

改變:

tools:text="home fragment" 

至:

android:text"home fragment"

tools標簽用於 Android Studio,設備看不到

問題出在 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