AndroidX - 工具栏汉堡菜单打开两次的故障

问题描述 投票:0回答:1

我在使用AndroidX工具条时遇到了一个小问题,我几乎是按以下方式设置工具条的...

app_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/toolbarBackground"
    app:theme="@style/AppTheme.ToolBar"
    app:popupTheme="@style/Theme.MaterialComponents.Light" />

menufragment_profile.xml。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/fragment_profile_menu_disconnect"
        android:title="@string/fragment_profile_menu_disconnect"
        app:showAsAction="never" />
</menu>

view_main.xml (其中包括工具条)

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- ToolBar -->
    <include
        layout="@layout/app_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <!-- Main -->
    <FrameLayout
        android:id="@+id/main_frame_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <!-- Bottom NavBar -->
    <!-- ... -->
</LinearLayout>

MainView.java

// [ ... ]
import androidx.appcompat.app.AppCompatActivity;

public final class MainView extends AppCompatActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.view_main);
        setSupportActionBar(findViewById(R.id.app_toolbar));
        // I've also tried to add "setActionBar(null);"
        // [ ... ]
    }

    // [ ... ]

    private void loadFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.main_frame_container, fragment)
                .commit();
    }
}

ProfileFragment.java

// [ ... ]
import androidx.fragment.app.Fragment;

public final class ProfileFragment extends Fragment {
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        setHasOptionsMenu(true);
        // loadProfileData();

        return view;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        // I've also tried "super..." before inflate
        inflater.inflate(R.menu.fragment_profile, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.fragment_profile_menu_disconnect) {
            // logOutAccount();
            return true;
        }
        return false;
    }
    // [ ... ]
}

而这是我在网上找不到解决的故障,也许是找错了关键词,但我试过很多 "句子"AndroidX - Toolbar menu open twice glitch

android toolbar androidx hamburger-menu visual-glitch
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.