在我的android应用中,我创建了这样的抽屉菜单:
import androidx.annotation.StyleableRes;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
public class MainNavigationDrawerFragmentActivity extends FragmentActivity
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar, R.string.application_name,
R.string.application_name) {
public void onDrawerClosed(View view) {
mToolbar.setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
mToolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu();
loadDraftsTotalNumber();
loadActiveCart();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
此处布局:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include
android:id="@+id/mainToolBar"
layout="@layout/tool_bar"/>
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="288dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@null"
android:listSelector="@drawable/list_selector"
android:paddingLeft="16dip"
android:paddingRight="16dip"/>
</androidx.drawerlayout.widget.DrawerLayout>
此处工具栏布局:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toolBarConstraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:theme="@style/ThemeOverlay.AppCompat.Dark"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
结果如下:
为什么汉堡包按钮不在工具栏中?在工具栏下。这是不正确的。
如果我这样更改布局:
<include
android:id="@+id/mainToolBar"
layout="@layout/tool_bar"/>
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
“汉堡”按钮消失了。