How do I get action buttons with custom layouts to be styled like standard action buttons in Android 3.0+

半腔热情 提交于 2020-01-02 05:37:26

问题


I'm having a bit of trouble with custom action buttons in the honeycomb+ action bar. I'm adding a menu item that uses a custom layout (using the android:actionLayout attribute). The reason for the custom layout is that I want a button that has two lines of text that can be updated dynamically.

However, I still want this action button to operate like the other standard buttons. By this I mean that the background fades in when the button is selected, and fades out again if it is unselected, all in the style of the platform (the colour seems to differ between different platforms/devices - I've seen both grey and blue versions)

I've tried using the action button style for the custom layout:

style="@android:style/Widget.ActionButton"

and I've tried setting the background for the custom layout to:

android:background="?android:attr/actionBarItemBackground"

but to no avail, and I'm kind of trying things fairly randomly as I can't find any documentation on how to do this (or if indeed it is even possible).

I know I can approximate this behaviour myself by setting the background, but it would be nice if I could just set the item to behave like a normal action button in terms of how it appears when the user interacts with it.

Anyone have any ideas?

Thanks in advance!


回答1:


Ah, sorry to answer my own question but I have just stumbled upon a way to do this. I was halfway there - you need your custom layout's style to inherit from ActionButton:

@android:style/Widget.ActionButton

but then you also need to make the layout clickable:

android:clickable="true"

for it to work. Using both of these makes the custom action buttons look just like the regular ones when you press them.

Hopefully that'll help someone trying to do this!



来源:https://stackoverflow.com/questions/7980293/how-do-i-get-action-buttons-with-custom-layouts-to-be-styled-like-standard-actio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!