gpt4 book ai didi

android - 更改 ActionMode 溢出图标

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:46 29 4
gpt4 key购买 nike

有没有办法在不更改“正常”ActionBar 图标的情况下更改 ActionMode 溢出图标?

最佳答案

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

好的。

让我们从定义一些样式开始。我将尝试解释为什么我们以这种方式定义它们:

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

在您的 Activity 中,定义一个方法 - 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 不受影响,因为它已经使用了旧属性。但是, Action 模式尚未创建(它将在我们从 onCreateActionMode(...) 返回 true 时创建)。当 Action 模式检查此属性值时,它会获取新的值。

还有更多...

Manish 给出的答案非常棒。我从来没有想过使用内容描述来找到确切的 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>

当我们调用 getTheme().applyStyle(R.style.ChangeOverflowToDark, true); 时,我们上面定义的 id 将被分配给 ImageButton/em>

我将在这里借用 Manish 的回答中的代码 fragment :

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

两全其美?

假设我们需要 R.drawable.overflow_menu_light 用于 ActionBarR.drawable.overflow_menu_dark 用于 ActionMode .

样式:

<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 - 但我们不需要 light 版本吗ActionBar?是的 - 我们将在 Activity 的 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 drawable。

对于这篇巨大的帖子,我深表歉意。我真的希望它能有所帮助。

关于android - 更改 ActionMode 溢出图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com