[英]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.