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. My code is as follows:
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>
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.
Change:
tools:text="home fragment"
To:
android:text"home fragment"
The tools
tag is for Android Studio, the device won't see it
The problem is in TextView tag, replace the tools:text to 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" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.