[英]How to get the Action Bar to display an icon and no title in both states of Navigation drawer?
I am trying to get my Action Bar to display an icon and not display any title while both states of the Navigation Drawer (open and closed) I read the documentation on developers android about how to modify the contents of the Action Bar when the drawer is visible. 我试图让我的操作栏显示一个图标,而不显示任何标题,同时导航抽屉的两种状态(打开和关闭)我阅读了开发人员android上有关如何在抽屉处于打开状态时修改操作栏内容的文档。可见。 As the page said, I created an instance of
ActionBarDrawerToggle
and set the icon and set title to null. 如页面所述,我创建了一个
ActionBarDrawerToggle
实例,并将图标设置为title并将其设置为null。 But still I am not able to see any icon and the app title appears as soon as I draw out the Navigation drawer. 但是,仍然无法看到任何图标,并且在我抽出“导航”抽屉后便会出现应用程序标题。 The following is the code I have used in
onCreate
of my activity 以下是我在活动的
onCreate
中使用的代码
mToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.drawer_open_or_close,
R.string.drawer_open_or_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.drawer_open_or_close);
getActionBar().setIcon(R.drawable.icon_144);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.drawer_open_or_close);
getActionBar().setIcon(R.drawable.icon_144);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
As you can see, I have set the Action bar title to R.string.drawer_open_or_close
, which is an empty string, during both completely open and completely closed state. 如您所见,在完全打开和完全关闭状态下,我已将操作栏标题设置为
R.string.drawer_open_or_close
,这是一个空字符串。 I have set the app icon in both cases as well. 在这两种情况下,我也都设置了应用程序图标。 What am I missing here?
我在这里想念什么?
Edit: I had forgotten to set the drawer toggle as DrawerListener
, which I did 编辑:我忘了设置抽屉切换为
DrawerListener
,我做了
drawerLayout.setDrawerListener(mToggle);
Now when I open Navigation drawer, the app force closes and the log cat says java.lang.NullPointerException
at 现在,当我打开导航抽屉时,应用程序强制关闭,日志猫在以下位置显示
java.lang.NullPointerException
getActionBar().setTitle(R.string.drawer_open_or_close);
Edit 2: I guess we cannot give a null string as title 编辑2:我想我们不能给一个空字符串作为标题
01-31 16:01:45.335 13944-13944/com.example.android.effectivenavigation E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.android.handsfree.MainPage$1.onDrawerOpened(MainPage.java:79)
at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:656)
at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:616)
at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1627)
at android.support.v4.widget.ViewDragHelper.setDragState(ViewDragHelper.java:873)
at android.support.v4.widget.ViewDragHelper$2.run(ViewDragHelper.java:335)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5162)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
I don't know if this is the best way but I ended up using a custom toolbar and then just manipulating the icon and title directly with findViewById()
. 我不知道这是否是最好的方法,但是我最终使用了一个自定义工具栏,然后直接使用
findViewById()
直接操作图标和标题。 I use a common toolbar that I include in each of my activity layouts. 我使用了包含在每个活动布局中的通用工具栏。
<ImageView
android:id="@+id/toolbarLogo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/logo"
android:adjustViewBounds="true"
android:padding="2dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/toolbarTitle"
android:textAppearance="@android:style/TextAppearance.Theme"
android:textColor="@color/titleTextColor"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:singleLine="true"
/>
</android.support.v7.widget.Toolbar>
Then I set what I need with: 然后,用以下命令设置所需的内容:
// Set the custom toolbar
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setTitle("");
}
TextView title = (TextView) findViewById(R.id.toolbarTitle);
title.setText(getString(R.string.app_long_name));
title.setTextColor(getResources().getColor(R.color.colorPrimary));
Hopefully that gives you some ideas. 希望能给你一些想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.