求助帖
android 在activity中設定一個按鈕,監聽點擊跳轉fragment,但是沒想到跳轉后兩個頁面內容同時展示了,大佬們這個問題我該怎么解決呢!!
@Override
public void onClick(View v) {
androidx.fragment.app.FragmentManager fragmentManager = getSupportFragmentManager();
androidx.fragment.app.FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.frg1:
Log.d(TAG, "點擊跳轉事件");
beginTransaction.replace(R.id.main_re, new BlankFragment());
beginTransaction.addToBackStack(null);
break;
}
beginTransaction.commit();
// moveTaskToBack(true);
}
這是MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/main_re"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/frg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="跳轉"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="248dp"
tools:layout_editor_absoluteY="295dp" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
這是運行結果

uj5u.com熱心網友回復:
先隱藏,再顯示uj5u.com熱心網友回復:
fragment本身就依附于Activity的,這不能稱之為跳轉,你只是吧fragment放進Activity的這個容器里,如果容器不能遮住Activity的所有布局,那不就是同時顯示了諾轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/285289.html
標籤:Android
上一篇:關于Kivy傳入圖片名稱的問題
