[英]Toggle the Navigation Drawer via the Action Bar title
我试图通过点击操作栏标题(这是当前Android Gmail应用程序的设置方式)允许用户在我的应用中打开/关闭导航抽屉。 此时,用户可以通过点击应用程序/抽屉图标或通过左右滑动将其滑入来切换抽屉。 但是,操作栏标题本身不可单击。 根据开发人员文档 ,当我们使用NAVIGATION_MODE_STANDARD
时,单击操作栏标题应该“使用具有项目ID android.R.id.home
的MenuItem将onOptionsItemSelected
调度到主机Activity”但由于某种原因我无法获得行为标题这条路。
我相信导航抽屉本身很好,但这是我如何设置动作栏:
private void configureActionBar(CharSequence mTitle) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.ic_blank);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {
0xFF004700, 0xFF002900
});
gd.setCornerRadius(0f);
actionBar.setBackgroundDrawable(gd);
// set the title of the action bar using the given title
actionBar.setTitle(mTitle);
}
任何建议将不胜感激!
如果你想通过点击ActionBar的Icon / Title来打开抽屉,我建议你使用支持库中提供的ActionBarDrawerToggle类( android.support.v4.app.ActionBarDrawerToggle )
参考: https : //developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html
使用示例:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
当捕获onOptionsItemSelected()中的事件时,必须将其传递给ActionBarDrawerToggle,以便它可以处理打开/关闭抽屉请求:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
如果在AndroidManifest.xml中设置应用程序主题属性,则显示图标/标题,如下所示:
<application
android:name=".SampleApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
res / values / styles.xml包含主题声明
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
它的工作原理是使用android.support.v7.app.ActionBarDrawerToggle,同时不推荐使用support.v4类。 请参见如何使用support-v7-appcompat库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.