Invisible ActionMode item icons in Theme.Sherlock.Light.DarkActionBar

风格不统一 提交于 2019-12-04 17:56:28

问题


When using Theme.Sherlock.Light.DarkActionBar (or Theme.Holo.Light.DarkActionBar, doesn't make a difference), the ActionMode (or "contextual ActionBar") that appears for example when selecting text, is by default styled the same way as in the standard dark theme, that is dark blue with light action icons.

However, when you try to select a text in a Dialog (which is light-styled in this theme, in contrast to the black ActionBar), an ActionMode styled as in the Light theme (white background) appears instead. The problem is, that its action icons are not dark as they should be, but light, making them effectively invisible.

This seems as if the background was taken from the light theme (because of the light dialog), but the icons were taken from the dark theme. Is this a bug in the Light.DarkActionBar theme? Can I do something about it?


回答1:


I was fighting with the same issue since last two days. Finally I came up with a workaround!

I am using a DialogFragment with an AlertDialog object. In the onCreateDialog() method, all we have to do is get the context of the parent activity and set its theme again to 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();

Now the icons in the Contextual ActionBar of the EditText have the right color.




回答2:


Forcing setTheme on the current context is not a good idea, as it corrupts the theme. To solve that you can use ContextThemeWrapper.

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



回答3:


I faced same problem. I use custom theme inherited from Theme.AppCompat.Light. In my theme I overrided actionModeTheme item by my custom style item. But I faced problem as your problem. To solve this problem just override android:alertDialogTheme item in your custom theme.

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

Note, it works since api 11 level.




回答4:


Problem:

App theme is inherited from android:Theme.Light, and there is no dedicated theme for AlertDialog, so that the ActionMode items are somehow invisible.


Solution:

1. Create a dedicated theme for 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>

Note: the most important line which makes the magic is <item name="android:textColor">@android:color/holo_blue_light</item>

2. Use the dedicated theme when building an AlertDialog.

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

Please have a look at the screenshot which shows before and after applying the theme.




回答5:


Add this to your theme:

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

And then add the necessary drawable resources to your project which can be found here: https://github.com/android/platform_frameworks_base/tree/master/core/res/res




回答6:


I tried abovementioned solutions, the only one that worked for me (I have app targeted for API 7, with AppCompat 21.0.3) is to set the dialog style to R.style.Theme_AppCompat. Yes, the stupid dialog is now black. This issue is also present in 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:


I had the same problem with application I'm maintaining The solution was very simple - I just had to change compileSdkVersion and targetSdkVersion to latest




回答8:


The solution is to use the ActionBar's context to create the AlertDialog:

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

Or using ActionBarSherlock/ActionBarCompat:

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

Then create the AlertDialog using this themed context:

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


来源:https://stackoverflow.com/questions/19741872/invisible-actionmode-item-icons-in-theme-sherlock-light-darkactionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!