Android 操作栏:自定义选项卡和溢出
我很难为操作栏实现自定义样式的选项卡:我需要使选项卡(我的意思是按钮)在正常状态和选定状态下使用自定义图形。
我已经能够使用 Tab.setIcon() 杀死所有本机样式
<style name="customActionBarTabStyle">
<item name="android:background">#00000000</item>
</style>
,然后使用 Tab.setIcon() 使选项卡看起来像我需要的方式,但我需要它们对切换做出反应(通过在两个 Drawable 之间切换 - 用于打开和关闭状态)。
我尝试创建一个像这样的可绘制选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_on" />
<item android:drawable="@drawable/btn_off" />
</selector>
但是选项卡在选择时不会切换到按下模式。
另外,我尝试在 TabListener.onTabSelected() 和 .onTabUnselected() 中调用 Tab.setIcon() - 也不走运。
有谁知道这个问题有一个好的解决方案吗?
另外,我需要显示一个自定义视图而不是溢出菜单 - 我已经在谷歌上搜索了一堆“提示”来重新思考我的 UI 以“遵循 Android 方式”,但问题是 UI 实际上不是我需要重新思考的 -这是客户的:)所以我真的很想找到一种方法来解决 ActionBar 的定制缺点。
非常感谢任何建议,提前致谢。
I'm having a hard time implementing custom-styled tabs for action bar: I need to make tabs (I mean buttons) use custom graphics for both normal and selected states.
I've been able to kill all the native styling using
<style name="customActionBarTabStyle">
<item name="android:background">#00000000</item>
</style>
and then use Tab.setIcon() to make the tabs look the way I need, but I need them to react to switching (by switching between two Drawables - for on and off state).
I've tried creating a Drawable selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_on" />
<item android:drawable="@drawable/btn_off" />
</selector>
but the tabs don't switch to pressed mode when selected.
Also, I've tried calling Tab.setIcon() in TabListener.onTabSelected() and .onTabUnselected() - no luck either.
Does anyone know a good solution to this one?
Also, I need to display a custom view instead of overflow menu - I've already googled up a bunch of "hints" to rethink my UI to "follow the Android way" but the problem is the UI is not actualy mine to rethink - it's customer's :) so I'd really like to find a way to workaround ActionBar's customization shortcomings.
Any suggestions are much appreciated, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用 state_selected 而不是 state_pressed,只是尝试在这里执行类似的操作(完全更改选项卡的背景图像),并且操作栏选项卡似乎只输入选定的 true/false 而不是按下状态...
编辑:似乎就像背景属性进入该状态一样,但选项卡上使用的文本颜色没有进入...
Try using state_selected instead of state_pressed, just trying to do something similar here (changing the background images for the tabs completely) and it seems like that the action bar tabs only enter selected true/false but not the pressed state...
EDIT: Seems like the background property enters that state, but not the text color used on the tabs...
我认为你必须使用 android:selectableItemBackground
看看这个它可能会有所帮助。
祝你好运
i think that you'll have to use android:selectableItemBackground
have a look at this it may help.
Good luck
只是为了使解决方案更加呈现:
@taf 表示解决方案:您需要设置
android:duplicateParentState=在用作选项卡自定义视图的父布局中设置“true”
。Just to make the solution more present:
@taf said the solution: you need to set the
android:duplicateParentState="true"
in the parent layout you use as a custom view for your tab.这是我的代码,选择选项卡时图标会发生变化,取消选择时图标会变回来。
还有这个
Here is my code and the icon change when selected a tab, and change back when unselected.
and this