简体   繁体   English

Android,如何在状态更改时更改操作栏徽标按钮?

[英]Android, How to change the actionbars logo button on state change?

So I'm wondering how one could implement to an action bar with a logo button, a different image per state.(pressed, released etc). 因此,我想知道如何通过徽标按钮(每个状态(按下,释放等))将其实现到带有徽标按钮的操作栏。

I know how to do this for a normal button, but I'm not sure if it's possible with the actionbars logo button? 我知道如何对普通按钮执行此操作,但是我不确定用actionbars徽标按钮是否可行?

And if it's not, how could one implement am action bar that supports this? 如果不是,那么如何实现支持此功能的动作栏呢? externals libs? 外部库?

Thank you. 谢谢。

Create a drawable xml file in res/drawable such as res/drawable/button_selector.xml res/drawable创建一个drawable xml文件,例如res/drawable/button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
      android:state_pressed="true" />
    <item android:drawable="@drawable/button_normal" />
</selector>

where button_pressed and button_normal are just regular png files in one of your drawable directories (eg/ drawables-hdpi ) 其中button_pressedbutton_normal只是可绘制目录之一中的常规png文件(例如, drawables-hdpi

Then in your menu.xml or in your code you can refer to @drawable/button_selector or R.drawable.button_selector and the image will change automatically when the button is pressed. 然后,在menu.xml或代码中,您可以引用@drawable/button_selectorR.drawable.button_selector ,按下按钮后图像将自动更改。

If you need to change the ActionBar button background you need to override the theme in your res/values/style.xml . 如果需要更改ActionBar按钮的背景,则需要覆盖res/values/style.xml的主题。

<style name="YourTheme.Sherlock" parent="@style/Theme.Sherlock">
    <item name="actionBarItemBackground">@drawable/actionbar_item_background_selector</item>
    <item name="android:actionBarItemBackground">@drawable/actionbar_item_background_selector</item>
</style>

Both lines matter: one for the SherlockActionBar and one for the standard ActionBar 这两行都很重要:一个用于SherlockActionBar,另一个用于标准ActionBar。

Then set the theme to your activity in the manifest: 然后将主题设置为清单中的活动:

   <activity
        android:name=".yourActivity"
        android:theme="@style/YourTheme.Sherlock" >
    </activity>

And here is actionbar_item_background_selector.xml to be placed in res/drawable 这是放置在res/drawable actionbar_item_background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Even though these two point to the same resource, have two states so the drawable     will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center"     />
    <item android:state_focused="true"  android:state_enabled="false"                                  android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/abs__list_focused_holo" />
    <item                                                                                          android:drawable="@android:color/transparent" />

</selector>

replace the background with the drawable of your choice where android:state_pressed="true" 用您选择的drawable替换背景,其中android:state_pressed="true"

It's a lot to do for just a background but that is how the ActionBar works. 仅背景就可以做很多事情,但这就是ActionBar工作方式。

you can use a selector to pick different image. 您可以使用选择器选择其他图像。 Android ImageButton with disabled UI feel may be help UI禁用的Android ImageButton可能会有所帮助

I think you are looking for this: 我认为您正在寻找:

final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);

This automatically makes the logo you have set look like its being pressed when its clicked. 这将自动使您设置的徽标看起来像在单击时被按下。 No need to have different images to show state change. 无需使用其他图像即可显示状态变化。 Thats a pretty easy way. 那是一个非常简单的方法。 Though if you are using ActionBarSherlock this won't work, I think. 我认为,尽管如果您使用的是ActionBarSherlock,将无法正常工作。 Strictly 4.0 I believe. 我相信绝对是4.0。 Check out the doc on ActionBar . ActionBar上查看文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM