定制ActionBar返回按钮

5

enter image description here enter image description here

<style name="MyActionBar.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/button_style</item>
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style> 

如何自定义操作栏的“向上导航”按钮。实际上,我希望当我点击它时,它的颜色会改变。例如WhatsApp,当我点击返回按钮时,按钮的背景颜色变为蓝色。我该怎么做?

2个回答

7

您需要为项目的自定义主题覆盖android:homeAsUpIndicator

<!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light">
          <item name="android:homeAsUpIndicator">@drawable/ic_action_up_caret</item>
    </style>

接下来,在您的清单文件中将您的自定义设置为应用程序主题(如果尚未这样做):

<application
        android:name="com.example.app"
        android:theme="@style/AppTheme" >

3
您可以通过编程方式设置图标的图像:
ActionBar actionBar = getActionBar();
//set icon
actionBar.setIcon(some Icon);

您可以在每次单击时更改此图标。

如果您想将颜色设置为图标:

ActionBar actionBar = getActionBar();
actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.niceBackgroundColor)));

我不想改变图标,我想要图标的背景颜色。 - Ahmet
您是指ActionBar的背景吗? - Daniel Budick
我只需要一个按钮的悬停事件,当我点击后退按钮时,背景颜色会改变。 - Ahmet
如果您使用WhatsApp,请点击homeUpIndicator按钮。当您点击时,您会看到它的背景变为蓝色。我该怎么做呢? - Ahmet
我不使用WhatsApp。但如果您想更改按钮的背景,则需要两个图像。例如,一个具有白色背景和一个具有蓝色背景。如果您单击该按钮,可以通过 setIcon() 更改图像。或者您可以采用Andre Perkins的解决方案,并通过xml完成。 - Daniel Budick

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