Android 工具栏切换按钮(汉堡包)在 Android pre Lollipop 中不起作用
Android Toolbar toggle button (hamburguer) not working in Android pre Lollipop
我已经创建了一个导航抽屉,就像这里图片中的一样
在 android 5+ 上一切正常,就像图像一样,但在 Lollipop 之前的版本中,菜单按钮 (hamburguer/toggle) 在单击时不会打开导航抽屉,并且工具栏会变暗当 navigatioDrawer 打开时。如何使 toogle 按钮起作用并从工具栏中删除阴影?
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:theme="@style/AppTheme.AppBarOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent">
<!-- Activity Content -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/homeContent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginTop="65dp"
android:src="@drawable/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:minWidth="250dp"
android:minHeight="200dp"
android:contentDescription="@string/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbltxtHome"
android:id="@+id/txtInfoHome"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
</RelativeLayout>
<!-- Navigation View -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
app:itemIconTint="@color/navDrawerIconColor"
android:background="@color/navDrawerBackground"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:itemTextColor="@color/navDrawerTextColor"
app:itemBackground="@drawable/drawer_item"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginTop="?attr/actionBarSize"
app:headerLayout="@layout/drawer_base_header"
app:menu="@menu/drawer_base_menu" />
</android.support.v4.widget.DrawerLayout>
这是我 Activity class
上的工具栏
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Toggle button
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
您仍在代码中的以下行中启用主页按钮作为切换按钮:
// enabling Toolbar bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawer.setDrawerListener(toggle);
toggle.syncState();
我已经创建了一个导航抽屉,就像这里图片中的一样
在 android 5+ 上一切正常,就像图像一样,但在 Lollipop 之前的版本中,菜单按钮 (hamburguer/toggle) 在单击时不会打开导航抽屉,并且工具栏会变暗当 navigatioDrawer 打开时。如何使 toogle 按钮起作用并从工具栏中删除阴影?
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:theme="@style/AppTheme.AppBarOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent">
<!-- Activity Content -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/homeContent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginTop="65dp"
android:src="@drawable/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:minWidth="250dp"
android:minHeight="200dp"
android:contentDescription="@string/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbltxtHome"
android:id="@+id/txtInfoHome"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
</RelativeLayout>
<!-- Navigation View -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
app:itemIconTint="@color/navDrawerIconColor"
android:background="@color/navDrawerBackground"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:itemTextColor="@color/navDrawerTextColor"
app:itemBackground="@drawable/drawer_item"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginTop="?attr/actionBarSize"
app:headerLayout="@layout/drawer_base_header"
app:menu="@menu/drawer_base_menu" />
</android.support.v4.widget.DrawerLayout>
这是我 Activity class
上的工具栏 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Toggle button
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
您仍在代码中的以下行中启用主页按钮作为切换按钮:
// enabling Toolbar bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawer.setDrawerListener(toggle);
toggle.syncState();