如何在导航抽屉中添加自定义操作栏?

7

我曾尝试使用自定义操作栏:

bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        bar.setCustomView(R.layout.xyz);

但导航抽屉变得不可见。

3个回答

11

我曾经遇到同样的问题,但我找到了一个解决方法。你需要设置:

getActionBar().setCustomView(R.layout.xyz);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);

接下来,它将显示导航抽屉和图标。

如果您不想让图标可见,可以将透明图片放置在drawable文件夹中(只需制作透明图片transparent.png并放置在drawable文件夹中)。您可以通过为操作栏定义新样式(styles.xml)来实现这一点:

<style name="Theme.MyAppTheme" parent="android:style/Theme.Holo.Light">
     <item name="android:actionBarStyle">@style/Theme.MyAppTheme.ActionBar</item>
</style>
<style name="Theme.MyAppTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
    <item name="android:icon">@drawable/transparent</item>
</style>

添加透明图标无效,请告诉我如何在上述情况下从操作栏中移除应用程序图标的解决方案。 - mark
@mark 用于移除应用程序图标。actionBar.setIcon(android.R.color.transparent); 可以实现此功能。 - Khurram Shehzad

1

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);

意思是设置ActionBar的显示选项,包括自定义视图、显示应用程序图标和显示向上导航箭头。

1
我做过的是: getActionBar().setCustomView(R.layout.xyz); getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接