有没有办法更改ActionMode溢出图标而不更改“普通”ActionBar的图标?更改ActionMode溢出图标
回答
你应该能够做到这一点使用样式:
ActionBarSherlock:
<style name="MyTheme" parent="Theme.Sherlock.Light">
<item name="actionOverflowButtonStyle">@style/MyTheme.OverFlow</item>
</style>
<style name="MyTheme.OverFlow" parent="Widget.Sherlock.ActionButton.Overflow">
<item name="android:src">@drawable/YOUR_ICON_GOES_HERE</item>
</style>
ActioBar:
<style name="MyTheme" parent="@android:style/Theme.Holo">
<item name="android:actionOverflowButtonStyle">@style/MyTheme.OverFlow</item>
</style>
<style name="MyTheme.OverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">@drawable/YOUR_ICON_GOES_HERE</item>
</style>
确保设定的MyTheme在清单中。
ImageButton
是用于显示菜单溢出该微件。 actionOverflowButtonStyle
用于造型ImageButton
。这种造型适用于ActionMenuPresenter。
private class OverflowMenuButton extends ImageButton implements ActionMenuChildView {
public OverflowMenuButton(Context context) {
super(context, null, com.android.internal.R.attr.actionOverflowButtonStyle);
...
}
}
ActionMenuPresenter
类是用于无论在action bar
和action modes
建设行动菜单。因此,通过覆盖主题文件将在两种模式下应用相同的样式。完成的唯一方法是编程方式,因为here为action bar
。
下面是如何它可以为action mode
溢出图标来完成的代码。您可以在ActionMode.Callback.onPrepareActionMode
方法中将drawable
分配给ImageButton
。
public class MainActivity extends Activity {
ViewGroup mDecorView;
public void onCreate(Bundle savedInstanceState) {
// Assign mDecorView to later use in action mode callback
mDecorView = (ViewGroup) getWindow().getDecorView();
}
private ActionMode.Callback mCallback = new ActionMode.Callback()
{
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
// We have to update the icon after it is displayed,
// hence this postDelayed variant.
// This is what I don't like, but it is the only way to move forward.
mDecorView.postDelayed(new Runnable() {
@Override
public void run() {
ArrayList<View> outViews = new ArrayList<View>();
// The content description of overflow button is "More options".
// If you want, you can override the style and assign custom content
// description and use it here.
mDecorView.findViewsWithText(outViews, "More Options", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if(!outViews.isEmpty()) {
View v = outViews.get(0);
if(v instanceof ImageButton) {
ImageButton btn = (ImageButton) v;
// Update the image here.
btn.setImageResource(R.drawable.custom);
}
}
}
}, 500);
return true;
}
}
}
我还需要弄清楚如何为我的默认动作条是不是在ActionMode,动作条可见改变了我的溢出图标只改变内部ActionMode-动作条的溢出图标(不,我不想改变我的ActionMode-Actionbar的背景!)
好的。
让我们从定义一些样式开始。我会尽量解释为什么我们在这种方式定义它们:
// This is just your base theme. It will probably include a lot more stuff.
// We are going to define the style 'OverflowActionBar' next.
<style name="BaseTheme" parent="android:Theme.Holo.Light">
....
....
....
<item name="android:actionOverflowButtonStyle">@style/OverflowActionBar</item>
</style>
// Assigning a parent to this style is important - we will inherit two attributes -
// the background (state-selector) and the content description
<style name="OverflowActionBar" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">@drawable/overflow_menu_light</item>
</style>
// Next up is an extension to our 'BaseTheme'. Notice the parent here.
<style name="ChangeOverflowToDark" parent="@style/BaseTheme">
<item name="android:actionOverflowButtonStyle">@style/OverflowActionMode</item>
</style>
// One last thing is to define 'OverflowActionMode'. Again, we inherit useful
// attributes by assigning 'Widget.Holo.ActionButton.Overflow' as the parent.
<style name="OverflowActionMode" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">@drawable/overflow_menu_dark</item>
</style>
所有我们与styles.xml
的工作已经完成。最后一点在运行时发生。我想你已经有了一个执行ActionMode.Callback
。
在你的活动中,定义一个方法 - changeOverflowIcon()
:
public void changeOverflowIcon() {
getTheme().applyStyle(R.style.ChangeOverflowToDark, true);
}
你将调用从ActionMode.Callback
实施onCreateActionMode(...)
这个方法:
public class CustomActionModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
changeOverflowIcon()
// other initialization
return true;
}
@Override
public boolean onPrepareActionMode(final ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {}
}
的解释了一下:
'BaseTheme'中的作业用于ActionBar
。它会选择可绘制的overflow_menu_light
,因为我们将其分配在应用程序的基本主题中。
getTheme().applyStyle(R.style.ChangeOverflowToDark, true)
第二个参数true
迫使当前主题覆盖用新的旧的属性。由于我们只在ChangeOverflowToDark
中定义了一个属性,因此它的值将被覆盖。 ActionBar
不受影响,因为它已经使用旧属性。但是,动作模式尚未创建(当我们从onCreateActionMode(...)
返回true
时将创建该动作模式)。当动作模式检查这个属性值时,它会得到新的属性值。
还有更多...
通过马尼什给出的答案是相当真棒。我本可以从未想过使用内容描述来找到确切的ImageButton
。 但是,如果您可以使用简单的findViewById()
找到ImageButton
?
下面是如何,您可以:
首先,我们需要唯一的ID。如果您的项目目前没有res/values/ids.xml
文件,请创建一个。添加一个新的ID:
<item type="id" name="my_custom_id" />
我在上面讨论的设置将保持不变。唯一的区别是在OverflowActionMode
风格:
<style name="OverflowActionMode" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">@drawable/overflow_menu_dark</item>
<item name="android:id">@id/my_custom_id</item>
</style>
我们上面定义的ID将被分配到当我们调用getTheme().applyStyle(R.style.ChangeOverflowToDark, true);
我在这里借用Manish的答案代码段的ImageButton
:
private ActionMode.Callback mCallback = new ActionMode.Callback()
{
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
mDecorView.postDelayed(new Runnable() {
@Override
public void run() {
ImageButton btn = (ImageButton) mDecorView.findViewById(R.id.my_custom_id);
// Update the image here.
btn.setImageResource(R.drawable.custom);
}
}, 500); // 500 ms is quite generous // I would say that 50 will work just fine
return true;
}
}
两全其美?
比方说,我们需要为ActionMode
R.drawable.overflow_menu_light
为ActionBar
和R.drawable.overflow_menu_dark
。
样式:
<style name="BaseTheme" parent="android:Theme.Holo.Light">
....
....
....
<item name="android:actionOverflowButtonStyle">@style/OverflowActionMode</item>
</style>
<style name="OverflowActionMode" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">@drawable/overflow_menu_dark</item>
<item name="android:id">@id/my_custom_id</item>
</style>
正如我们的风格定义,则ActionBar
会挑R.drawable.overflow_menu_dark
- 但不要我们需要光版为ActionBar
?是的 - 我们将指派,在活动的onPrepareOptionsMenu(Menu)
回调:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ImageButton ib = (ImageButton)
getWindow().getDecorView()
.findViewById(R.id.my_custom_id);
if (ib != null)
ib.setImageResource(R.drawable.overflow_menu_light);
}
}, 50L);
return super.onPrepareOptionsMenu(menu);
}
在此,我们这样做是因为onPrepareOptionsMenu(Menu)
前,ImageButton
不会被创建。现在
,我们不需要处理ActionMode
- 因为它会选择从主题dark
绘制。
我对这篇巨大的文章表示歉意。我真的希望它有帮助。
这只是一个了不起的解释!你的第一个教程工作得很好!在我看来,这是一种比处理Handlers和Runnables更美丽的方式....谢谢! :)) – Frame91
试用了样式的第一个解决方案,适用于Android 5.0以前的版本,但不适用于Android Lollipop 5.0。对此有何更新? –
- 1. AppCompat工具栏:更改ActionMode中的溢出图标颜色
- 2. 更改ActionMode的标题的Android
- 3. ActionBarSherlock图标溢出
- 4. 更改操作栏中的溢出图标
- 5. 更改代码中的Android ActionBar溢出图标
- 6. 如何更改Android溢出菜单图标
- 7. 更改工具栏溢出图标颜色
- 8. 溢出按钮无法更改为添加图标
- 9. Android |更改溢出图标(3点菜单)的颜色?
- 10. 将动作栏溢出图标更改为黑色版本
- 11. 如何关闭更改上的ActionMode FragmentTab
- 12. WPF TabControl与溢出图标
- 13. Genymotion动作溢出图标
- 14. Android ActionMode图标颜色白色
- 15. Theme.Sherlock.Light.DarkActionBar中的隐形ActionMode项图标
- 16. 如何更改上下文操作栏的溢出图标而不更改其标准操作栏?
- 17. 标准UIBarButtonItem溢出菜单图标
- 18. 溢出图标3点不出现
- 19. Android动作视图溢出图标
- 20. 溢出标签
- 21. 溢出`标签`
- 22. 更改CSS溢出隐藏行为
- 23. 如何更改溢出menuitem的背景
- 24. 更改字体颜色上溢出
- 25. 如何在操作栏中将溢出图标更改为白色?
- 26. 如何更改工具栏导航和溢出菜单图标(appcompat v7)?
- 27. 更改图标
- 28. 更改图标
- 29. 图像溢出
- 30. Eclipse传出更改星号图标
是的,我为ActionBar做了这些,但我需要Holo。ActionBar的黑色图标和ActionMode的Holo.Light图标。 – TeKo