片段替换整个屏幕

o2gm4chl  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(281)

我有下面的结构和片段replace/add不仅替换了它的主机视图,还替换了整个屏幕。什么会导致这种情况?
无数据的主活动布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.MainActivity">

    <FrameLayout
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="12"
        app:defaultNavHost="true" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        app:labelVisibilityMode="unlabeled"
        android:background="@drawable/transparent_button"
        app:onNavigationItemSelected="@{viewModel::onNavigationItemSelected}"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:menu="@menu/bottom_navigation_menu" />
</LinearLayout>

碎片布局

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize"
    tools:context=".fragment.HomeFragment">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="20dp"
        android:background="@drawable/transparent_button"
        android:onClick="@{viewModel::onFloatingButtonClicked}"
        android:src="@drawable/ic_baseline_add_24" />
</FrameLayout>

创建时的主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    mainViewModel = ViewModelProviders.of(this, new ViewModelFactory(getApplication())).get(MainViewModel.class);
    activityMainBinding.setViewModel(mainViewModel);
    activityMainBinding.executePendingBindings();

    fragmentList.put(FragmentsEnum.HOME.getValue(), new HomeFragment());
    fragmentList.put(FragmentsEnum.PROFILE.getValue(), new ProfileFragment());

    Fragments.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment, fragmentList.get(FragmentsEnum.HOME.getValue()), FragmentsEnum.HOME.getValue()); // DEFAULT FRAGMENT
}

碎片利用率

public static void addFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
    if (!manager.isStateSaved()) {
        manager.beginTransaction().add(containerId, fragment, tag).commit();
    }
}

在主活动视图上,当活动开始时,我将主片段设置为活动。但它隐藏了我的底部导航栏,取代了整个屏幕。

3duebb1j

3duebb1j1#

我认为使用linearlayout作为fragment和bottomnav的根元素是一个问题。尝试将其替换为constraintlayout或coordinatorlayout。如果这些都不起作用,请尝试从碎片中移除顶部填充。

l3zydbqr

l3zydbqr2#

您的framelayout将与bottomnavigationview重叠。此外,您应该为您的导航主机使用fragmentcontainerview。您希望将fragmentcontainerview显式置于bottomnavigationview之上。我将您的线性布局更改为具有适当约束的约束布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_fragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
    app:defaultNavHost="true" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:labelVisibilityMode="unlabeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:background="@drawable/transparent_button"
    app:onNavigationItemSelected="@{viewModel::onNavigationItemSelected}"
    app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
    app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

相关问题