【问题标题】:How to change the background color of a TextView inside of an ActionMode如何更改 ActionMode 内 TextView 的背景颜色
【发布时间】:2015-07-22 10:52:55
【问题描述】:

我的操作栏中有一个TextView,如下所示:

当我长按关联的ListView 中的项目时,我已配置逻辑使其覆盖ToolbarActionMode。这最终看起来如下所示:

这基本上是正确的,除了ActionMode中的TextView标题周围的恼人背景。

我尝试了很多事情,从更改我的styles.xml 文件中的Widget.ActionMode 样式,到实际将actionModeStyle 设置为特定样式。不过,似乎没有任何影响TextView

我已经阅读了How to change ActionMode background color in Android,但似乎我所做的任何事情都不会影响这个背景。

旁注:如果有某种方法可以在 Android 设备上查看呈现的样式层次结构,那就太好了。层次结构查看器让我了解了大约 1/3 的路径,但它没有任何样式信息。有谁知道我可以用来调试这个问题的工具吗?

styles.xml(仅相关内容):

<style name="Material" parent="Theme.AppCompat.Light">
    <item name="textHeaderMaxLines">@integer/text_header_max_lines</item>
    <item name="trackAbstractMaxLines">@integer/track_abstract_max_lines</item>
    <item name="activatableItemBackground">@drawable/activatable_item_background</item>

    <!-- ActionMode Styles -->
    <item name="android:windowActionModeOverlay">true</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeBackground">@color/app_green_dark</item>
</style>
<style name="Material.AppBar" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColorPrimaryInverse">@android:color/white</item>
    <item name="android:actionMenuTextColor">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:background">@color/app_green</item>
    <item name="android:color">@android:color/white</item>
    <item name="android:textColor">@android:color/white</item>
</style>
<style name="Material.AppBar.ActionMode" parent="Widget.AppCompat.Light.ActionMode.Inverse">
    <item name="android:background">@color/app_green_dark</item>
</style>

【问题讨论】:

  • 我不确定我是否理解你的问题,如果你需要根据你的 editText 状态更改背景,你可以在你的 editText 背景中使用state 来做到这一点
  • @MuhannadFakhouri 这不是EditText,而是ActionMode 中的默认TextView。我在ActionMode 对象上使用setTitle() 设置文本本身。我想知道如何设置样式,因为我从其他 SO 帖子中收集到的当前解决方案似乎没有任何作用。

标签: android android-actionbar styles android-actionmode


【解决方案1】:

我觉得你可以试试用hack的方式来改变背景:

通过 Java:

  1. 使用hierarchyviewer.bat分析布局,关注ActionMode视图,可以看到如下:(标题ID:action_bar_title,副标题ID:action_bar_subtitle

2.通过我们在上面找到的ID找到指定的TextView,然后为所欲为!

/**
 * To hack the title and subTitle in ActionMode
 */
private void hack() {
    int titleID = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    View titleView = getWindow().getDecorView().findViewById(titleID);
    if ((titleView != null) && (titleView instanceof TextView)) {
        ((TextView) titleView).setBackgroundColor(Color.RED);
    }
    int subTitleID = Resources.getSystem().getIdentifier("action_bar_subtitle", "id", "android");
    View subTitleView = getWindow().getDecorView().findViewById(subTitleID);
    if ((subTitleView != null) && (subTitleView instanceof TextView)) {
        ((TextView) subTitleView).setBackgroundColor(Color.GREEN);
    }
}

3.ActionMode创建时可以调用该方法,例如:onCreateActionMode()中。最终结果:

通过样式:

或者你可以尝试设置一个android:actionModeStyle来控制标题和子标题背景,但是android:background似乎不适合它:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionModeBackground">@android:color/white</item>
    <item name="android:actionModeStyle">@style/TestActionModeStyle</item>
</style>

<style name="TestActionModeStyle">
    <item name="android:titleTextStyle">@style/TestTitleStyle</item>
    <item name="android:subtitleTextStyle">@style/TestSubTitleStyle</item>
</style>

<style name="TestTitleStyle">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:background">@android:color/holo_red_dark</item>
</style>

<style name="TestSubTitleStyle">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:background">@android:color/holo_green_dark</item>
</style>

编辑:

顺便说一句:

我认为最灵活的方式是自定义你自己的ActionMode视图,这很容易实现,只需设置一个自定义视图:

@Override
public boolean onCreateActionMode(ActionMode pMode, Menu pMenu) {
    RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_layout, null);
    findView(...);
    initView(...);
    pMode.setCustomView(layout);
    return true;
}

【讨论】:

  • 谢谢,@Bill。有机会我会尽快检查一下。
  • 按照建议,我最终创建了一个自定义视图。感谢@Bill 的回答。
  • @jwir3 是的,我在我的项目中使用相同的方式!
猜你喜欢
  • 2014-01-13
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 2014-06-12
  • 2011-06-05
相关资源
最近更新 更多