我如何获得自定义布局的操作按钮来设置样式就像是Android的标准动作按钮3.0+按钮、自定义、如何获得、样式

2023-09-12 21:56:28 作者:冷巷﹏雨未停

我有一点麻烦,在蜂窝+操作栏自定义操作按钮。我添加使用自定义布局的菜单项(使用了android:actionLayout属性)。究其原因,自定义布局是,我想一个按钮,有两行文字可以动态更新。

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.

任何人有什么想法?

在此先感谢!

推荐答案

嗯,不好意思回答我的问题,但我刚才偶然发现了一个办法做到这一点。我是半路那里 - 你需要你的自定义布局的风格,从ActionButton继承:

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"

为它工作。使用这两种使自定义操作按钮看起来就像普通的人,当你preSS他们。

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!

 
精彩推荐
图片推荐