【问题标题】:Invisible ActionMode item icons in Theme.Sherlock.Light.DarkActionBarTheme.Sherlock.Light.DarkActionBar 中不可见的 ActionMode 项图标
【发布时间】:2013-11-02 12:25:32
【问题描述】:

当使用Theme.Sherlock.Light.DarkActionBar(或Theme.Holo.Light.DarkActionBar,没有区别)时,例如在选择文本时出现的ActionMode(或“上下文ActionBar”)默认样式与标准中的样式相同深色主题,即带有浅色动作图标的深蓝色。

但是,当您尝试在 Dialog 中选择文本(在此主题中为浅色样式,与黑色 ActionBar 形成对比)时,会出现与浅色主题(白色背景)中样式相同的 ActionMode。问题是,它的动作图标并不像应有的那样暗,而是亮,使它们实际上是不可见的。

这看起来好像背景取自浅色主题(因为浅色对话框),但图标取自深色主题。这是Light.DarkActionBar 主题中的错误吗?我能做点什么吗?

【问题讨论】:

  • 我没有找到解决这种情况的方法,看来AlertDialog的actionbar主题只能和它自己一样。但是一种解决方法是使用另一个 DialogFragment 来显示 AlertDailog 的内容,这样我们就可以毫无问题地拥有 Light.DarkActionBar 动作模式

标签: android android-theme


【解决方案1】:

自从过去两天以来,我一直在与同样的问题作斗争。最后我想出了一个解决方法!

我正在使用 DialogFragmentAlertDialog 对象。在onCreateDialog() 方法中,我们所要做的就是获取父活动的上下文并将其主题再次设置为Theme.Light

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    //get the parent activity context and set it's theme again.
    Context ctx = getActivity();
    ctx.setTheme(android.R.style.Theme_Holo_Light);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.your_dialog_layout, null, false);
    builder.setView(view);

    //your other code for the dialog
    //

    return builder.create();

现在EditText 的上下文操作栏中的图标颜色正确。

【讨论】:

  • 谢谢,这真的很有帮助!只有一件事,您可能想说 将其主题设置为 Theme.Light - 就像在代码 sn-p 中一样。
  • 哦,是的,我编辑了我的答案,但您可以将主题设置为 DarkActionBar 以获得深蓝色 CAB。 :)
  • Light.DarkActionBar 对我不起作用,图标在上面的白色背景上是白色的。只有 Light 主题才能解决问题。
  • 这是一个糟糕的答案,原因有两个:首先它使用原生 API11 样式,如果使用 ActionBarSherlock 或 ActionBarCompat,它将不兼容。其次,它不仅改变了对话框主题:它还改变了活动主题,因此您需要注意在对话框关闭时重新设置主题。
  • 这是唯一对我有帮助的答案,而且我没有遇到任何破坏活动主题的问题
【解决方案2】:

在当前上下文中强制setTheme 不是一个好主意,因为它会破坏主题。要解决这个问题,您可以使用ContextThemeWrapper

Context themedContext = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light);
final AlertDialog dialog = new AlertDialog.Builder(themedContext)

【讨论】:

  • 这不起作用,图标仍然隐藏。在 Nexus 7 4.4.2 中测试。
  • 已验证,也不适用于我的 4.4.4 CyanogenMod。
【解决方案3】:

我遇到了同样的问题。 我使用从Theme.AppCompat.Light 继承的自定义主题。在我的主题中,我用我的自定义样式项覆盖了actionModeTheme 项。但我面临的问题是你的问题。 要解决此问题,只需在您的自定义主题中覆盖 android:alertDialogTheme 项目。

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
        ...
        <item name="android:alertDialogTheme">@style/AppBaseTheme</item>
        ...
</style>

<style name="AlertDialogTheme">
        ...
        <item name="android:actionModeStyle">@style/ActionModeStyle</item>
        ...
</style>
<style name="ActionModeStyle" parent="Widget.AppCompat.Base.ActionMode">
            ...
            <item name="android:background">@color/gray</item>
            ...
</style>

注意,它从 api 11 级别开始工作。

【讨论】:

  • 没有帮助。图标仍然隐藏。操作系统 4.1.2
【解决方案4】:

问题:

应用主题继承自android:Theme.Light,并且没有专门用于AlertDialog的主题,因此ActionMode项在某种程度上是不可见的。


解决方案:

1.为 AlertDialog 创建一个专用主​​题;

<style name="AppTheme" parent="android:Theme.Light">
</style>
<style name="AlertDialogTheme" parent="AppTheme">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textColor">@android:color/holo_blue_light</item>
</style>

注意:最重要的一行是&lt;item name="android:textColor"&gt;@android:color/holo_blue_light&lt;/item&gt;

2。构建 AlertDialog 时使用专用主题。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);

请看一下应用主题前后的截图。

【讨论】:

    【解决方案5】:

    将此添加到您的主题中:

        <item name="android:actionModeCutDrawable">@drawable/ic_menu_cut_holo_light</item>
        <item name="android:actionModeCopyDrawable">@drawable/ic_menu_copy_holo_light</item>
        <item name="android:actionModePasteDrawable">@drawable/ic_menu_paste_holo_light</item>
        <item name="android:actionModeSelectAllDrawable">@drawable/ic_menu_selectall_holo_light</item>
    

    然后将必要的可绘制资源添加到您的项目中,可以在这里找到:https://github.com/android/platform_frameworks_base/tree/master/core/res/res

    【讨论】:

    • 这是唯一对我有帮助的解决方案
    【解决方案6】:

    我尝试了上述解决方案,唯一对我有用的解决方案(我的应用程序针对 API 7,AppCompat 21.0.3)是将对话框样式设置为 R.style.Theme_AppCompat。是的,愚蠢的对话框现在是黑色的。 AppCompat 22 中也存在此问题。

        final Context themedContext = new ContextThemeWrapper(activity, R.style.Theme_AppCompat);
        builder = new AlertDialog.Builder(themedContext);
        contents = ((LayoutInflater) themedContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.tag_editor, null);
        builder.setView(contents);
    

    【讨论】:

      【解决方案7】:

      我在维护的应用程序遇到了同样的问题 解决方案非常简单 - 我只需将 compileSdkVersion 和 targetSdkVersion 更改为最新

      【讨论】:

        【解决方案8】:

        解决方法是使用ActionBar的上下文来创建AlertDialog:

        Context themedContext = getActivity().getActionBar().getThemedContext();
        

        或使用 ActionBarSherlock/ActionBarCompat:

        Context themedContext = getActivity().getSupportActionBar().getThemedContext();
        

        然后使用这个主题上下文创建 AlertDialog:

        AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
        

        【讨论】:

        • 这将使alertDialog使用darkTheme,而不是lightTheme
        • 对我不起作用,在我的 Android 4.4.4 Cyanogenmod 上不起作用
        • @henry74918 这将使您的对话与您的活动主题保持一致。如果您想要一些自定义/不同的东西,您应该阅读其他强制主题的答案。
        猜你喜欢
        • 1970-01-01
        • 2018-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多