更改ActionMode溢图标 [英] Change ActionMode Overflow icon

查看:170
本文介绍了更改ActionMode溢图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来改变ActionMode溢图标不改变图标的​​常规动作条?

解决方案
  

我还需要弄清楚如何只改变ActionMode,动作条里面的溢图标,因为我改变了我的溢图标的默认动作条是不是在ActionMode-动作条可见(不,我不'吨要改变我的ActionMode-动作条的背景!)

好了。

让我们先从定义一些样式。我会尝试,并解释为什么我们定义它们以这种方式:

  //这只是你的基本主题。它可能会包括更多的东西。
//我们要定义样式OverflowActionBar的下。

<样式名称=BaseTheme父=机器人:Theme.Holo.Light>
    ....
    ....
    ....
    <项目名称=机器人:actionOverflowButtonStyle> @风格/ OverflowActionBar< /项目>
< /风格>

//指定一名家长这种风格是很重要的 - 我们将继承两个属性 - 
//背景(国家选择器)和内容描述

<样式名称=OverflowActionBar父=@安卓风格/ Widget.Holo.ActionButton.Overflow>
    <项目名称=机器人:SRC> @可绘制/ overflow_menu_light< /项目>
< /风格>

//接下来是一个扩展我们的BaseTheme。请注意这里的家长。

<样式名称=ChangeOverflowToDark父=@风格/ BaseTheme>
    <项目名称=机器人:actionOverflowButtonStyle> @风格/ OverflowActionMode< /项目>
< /风格>

//最后一件事是确定OverflowActionMode。同样,我们继承了有用的
//属性通过指定Widget.Holo.ActionButton.Overflow作为父。

<样式名称=OverflowActionMode父=@安卓风格/ Widget.Holo.ActionButton.Overflow>
    <项目名称=机器人:SRC> @可绘制/ overflow_menu_dark< /项目>
< /风格>
 

styles.xml 我们所有的工作已经完成。在最后位发生在运行时。我想你已经拥有的 ActionMode.Callback 的实施。

在你的活动,定义一个方法 - changeOverflowIcon()

 公共无效changeOverflowIcon(){
    。getTheme()applyStyle(R.style.ChangeOverflowToDark,真正的);
}
 

您会被要求从 onCreateActionMode这种方法(...) ActionMode.Callback 实施

 公共类CustomActionModeCallback实现ActionMode.Callback {

    @覆盖
    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
        changeOverflowIcon()

        //其他初始化

        返回true;
    }

    @覆盖
    在prepareActionMode公共布尔(最终ActionMode模式,菜单菜单){
        返回true;
    }

    @覆盖
    公共布尔onActionItemClicked(ActionMode模式,菜单项项){
        返回false;
    }

    @覆盖
    公共无效onDestroyActionMode(ActionMode模式){}
}
 

解释了一下:

在BaseTheme的任务是为动作条。它会挑绘制 overflow_menu_light ,因为我们赋予它在你的应用程序的基本主题。

  getTheme()。applyStyle(R.style.ChangeOverflowToDark,真)
 

第二个参数强制当前的主题覆盖用新的旧的属性。因为我们只定义在 ChangeOverflowToDark 一个属性,它的值将被覆盖。该动作条不会受到影响,因为它已经使用了旧的属性。不过,行动模式尚待建立(如果我们回到它会创建 onCreateActionMode(...))。当动作模式将检查该属性值,它获得了新的。

还有更多...

由马尼什给出的答案是相当真棒。我也从来没有想过使用内容描述准确的找到了的ImageButton 的。 但是,如果你能找到的ImageButton 用一个简单的 findViewById()

下面是如何您可以:

首先,我们需要的唯一ID。如果您的项目目前没有一个 RES /价值/ ids.xml 文件,创建一个。添加一个新的ID给它:

 <项目类型=IDNAME =my_custom_id/>
 

余上面讨论的设置将保持不变。唯一的区别将在 OverflowActionMode 风格:

 <样式名称=OverflowActionMode父=@安卓风格/ Widget.Holo.ActionButton.Overflow>
    <项目名称=机器人:SRC> @可绘制/ overflow_menu_dark< /项目>
    <项目名称=机器人:ID> @ ID / my_custom_id< /项目>
< /风格>
 

我们在上面定义的ID将被分配给的ImageButton 当我们调用 getTheme()。applyStyle(R.style.ChangeOverflowToDark ,真);

我会从Manish的答案在这里借用code段

 私人ActionMode.Callback mCallback =新ActionMode.Callback()
{
    @覆盖
    在prepareActionMode公共布尔(ActionMode模式,菜单菜单)
    {

        mDecorView.postDelayed(新的Runnable(){

            @覆盖
            公共无效的run(){
                的ImageButton BTN =(的ImageButton)mDecorView.findViewById(R.id.my_custom_id);
                //这里更新图像。
                btn.setImageResource(R.drawable.custom);
            }
        },500); // 500毫秒是相当慷慨//我要说的是,50工作得很好

        返回true;
    }
}
 

两全其美?

让我们说,我们需要 R.drawable.overflow_menu_light 动作条 R.drawable .overflow_menu_dark ActionMode

样式:

 <样式名称=BaseTheme父=机器人:Theme.Holo.Light>
    ....
    ....
    ....
    <项目名称=机器人:actionOverflowButtonStyle> @风格/ OverflowActionMode< /项目>
< /风格>

<样式名称=OverflowActionMode父=@安卓风格/ Widget.Holo.ActionButton.Overflow>
    <项目名称=机器人:SRC> @可绘制/ overflow_menu_dark< /项目>
    <项目名称=机器人:ID> @ ID / my_custom_id< /项目>
< /风格>
 

根据定义我们的风格,动作条将选择 R.drawable.overflow_menu_dark - 但我们不需要的的版本为动作条?是的 - 我们将指派,在活动的在prepareOptionsMenu(菜单)回调:

  @覆盖
公共布尔prepareOptionsMenu(功能菜单)在{

    新的处理程序()。postDelayed(新的Runnable(){
        @覆盖
        公共无效的run(){
            的ImageButton IB =(的ImageButton)
                                 getWindow()。getDecorView()
                                .findViewById(R.id.my_custom_id);
            如果(IB!= NULL)
                ib.setImageResource(R.drawable.overflow_menu_light);
        }
    },50L);
    返回super.on prepareOptionsMenu(菜单);
}
 

我们这样做是因为这里以前在prepareOptionsMenu(菜单)的ImageButton 不会有被创建。

现在,我们的的需要处理的 ActionMode - 因为这会挑从主题绘制。

我的道歉这个庞大的职位。我的真正的希望它能帮助。

Is there a way to change the ActionMode Overflow icon without changing the icon for the "normal" ActionBar?

解决方案

I still need to figure out how to only change the Overflow-Icon inside of the ActionMode-Actionbar as I changed my Overflow-Icon in the default-Actionbar which is not visible in the ActionMode-Actionbar (and no, I don't want to change the background of my ActionMode-Actionbar!)

Okay.

Let's start with defining some styles. I will try and explain why we are defining them in this fashion:

// 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>

All our work with styles.xml is done. The very last bit happens at runtime. I suppose you already have an implementation of ActionMode.Callback.

In your activity, define a method - changeOverflowIcon():

public void changeOverflowIcon() {
    getTheme().applyStyle(R.style.ChangeOverflowToDark, true);
}

You will be calling this method from onCreateActionMode(...) of your ActionMode.Callback implementation:

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) {}
}

A bit of explanation:

The assignment in 'BaseTheme' is for the ActionBar. It will pick the drawable overflow_menu_light since we are assigning it in the base theme of your app.

getTheme().applyStyle(R.style.ChangeOverflowToDark, true) 

The second argument true forces the current theme to override the old attributes with the new ones. Since we only define one attribute in ChangeOverflowToDark, its value is overwritten. The ActionBar is not affected because it has already used the old attribute. But, the action mode is yet to be created (it will be created when we return true from onCreateActionMode(...)). When the action mode checks for this attributes value, it gets the new one.

There's more...

The answer given by Manish is quite awesome. I could have never thought of using the content description to find the exact ImageButton. But what if you could find the ImageButton using a straightforward findViewById()?

Here's how you can:

First, we will need unique ids. If your project doesn't currently have a res/values/ids.xml file, create one. Add a new id to it:

<item type="id" name="my_custom_id" />

The setup I discussed above will remain the same. The only difference will be in OverflowActionMode 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>

The id we defined above will be assigned to the ImageButton when we call getTheme().applyStyle(R.style.ChangeOverflowToDark, true);

I'll borrow the code snippet from Manish's answer here:

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;
    }  
}

Best of both worlds?

Let's say we need R.drawable.overflow_menu_light for ActionBar and R.drawable.overflow_menu_dark for ActionMode.

Styles:

<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>

As defined in our style, the ActionBar will pick R.drawable.overflow_menu_dark - but don't we need the light version for the ActionBar? Yes - we will assign that in the activity's onPrepareOptionsMenu(Menu) callback:

@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);
}

We are doing this here because before onPrepareOptionsMenu(Menu), the ImageButton would not have been created.

Now, we don't need to deal with ActionMode - because it will pick the dark drawable from the theme.

My apologies for this gigantic post. I really hope it helps.

这篇关于更改ActionMode溢图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆