You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

导航抽屉点击后Fragment覆盖主布局求助:需隐藏主布局显示Fragment

Hey there! Let's fix this issue where your WeatherFragment is showing on top of content_main instead of replacing it. Here's how to tackle it step by step across your files:

解决导航抽屉点击菜单项时隐藏content_main仅显示WeatherFragment的问题

1. 调整MainActivity.java的菜单项点击逻辑

First off, in the code block where you handle navigation drawer item clicks, you need to add logic to hide the content_main layout alongside loading the WeatherFragment.

Suppose your original fragment loading code looks like this:

getSupportFragmentManager().beginTransaction()
    .replace(R.id.fragment_container, new WeatherFragment())
    .commit();

Modify it to include hiding content_main:

// Locate the root layout of content_main (assign an id like ll_content_main if it doesn't have one)
View contentMain = findViewById(R.id.content_main);
contentMain.setVisibility(View.GONE);

// Load WeatherFragment into a main container (preferably a FrameLayout in activity_main.xml, e.g., with id fl_main_container)
getSupportFragmentManager().beginTransaction()
    .replace(R.id.fl_main_container, new WeatherFragment())
    .commit();

If you want to restore content_main when the user presses back, add this override:

@Override
public void onBackPressed() {
    WeatherFragment weatherFragment = (WeatherFragment) getSupportFragmentManager().findFragmentById(R.id.fl_main_container);
    if (weatherFragment != null && weatherFragment.isVisible()) {
        View contentMain = findViewById(R.id.content_main);
        contentMain.setVisibility(View.VISIBLE);
        getSupportFragmentManager().beginTransaction().remove(weatherFragment).commit();
    } else {
        super.onBackPressed();
    }
}

2. Optimize content_main.xml's layout structure

Make sure the root layout of content_main has a unique id so you can reference it easily in code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <!-- Your original content_main content here -->

</LinearLayout>

3. Ensure WeatherFragment's layout fills the parent

Check fragment_weather.xml's root layout to make sure it takes up the entire screen once content_main is hidden:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Your WeatherFragment content here -->

</LinearLayout>

Alternative Approach: Replace the entire main container

If your activity_main.xml is structured like this:

<DrawerLayout ...>
    <!-- Main content area -->
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical">
        <include layout="@layout/app_bar_main" />
        <include layout="@layout/content_main" />
        <!-- Add a FrameLayout as the fragment container -->
        <FrameLayout 
            android:id="@+id/fl_main_container" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:visibility="gone" />
    </LinearLayout>
    <!-- Navigation drawer -->
    <NavigationView ... />
</DrawerLayout>

When a menu item is clicked, set content_main to GONE, set fl_main_container to VISIBLE, then load the WeatherFragment into this container. This way, the fragment completely replaces the content_main area.

After these changes, clicking a menu item will hide content_main and show only the WeatherFragment as expected!

内容的提问来源于stack exchange,提问作者Przemo

火山引擎 最新活动