2013-11-02 20 views
14

使用Theme.Sherlock.Light.DarkActionBar(或Theme.Holo.Light.DarkActionBar,没有区别)时,默认情况下会出现例如在选择文本时出现的ActionMode(或“上下文ActionBar”)风格与标准黑暗主题相同,即深蓝色与轻微动作图标。Theme.Sherlock.Light.DarkActionBar中的隐形ActionMode项图标

但是,当您尝试选择对话框中的文本(与此主题相比,它与黑色的ActionBar相比)时,会出现与Light主题(白色背景)中样式相同的ActionMode。问题在于,它的行动图标并不是它们应该的黑暗,而是光线,使它们看不见。

enter image description here

这好像背景是从光的主题拍摄(因为光线对话的),但是从黑暗的主题拍摄的图标。这是Light.DarkActionBar主题中的错误吗?我能做点什么吗?

+0

我没有找到针对这种情况的解决方案,它似乎AlertDialog的操作栏主题只能与本身相同。但是一种解决方法是使用另一个DialogFragment来显示AlertDailog的内容,这样我们可以有一个Light.DarkActionBar动作模式没有问题 – henry74918

回答

9

自从最近两天以来,我一直在为同样的问题而战。最后我想出了一个解决方法!

我正在使用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的Contextual ActionBar中的图标具有正确的颜色。

+0

谢谢,这真的有帮助!只有一件事,你可能想说它是Theme.Light_的主题 - 就像代码片段一样。 – Natix

+1

哦,是的,我编辑了我的答案,但您可以将主题设置为DarkActionBar以获得深蓝色的CAB。 :) –

+1

Light.DarkActionBar不适合我,这些图标在白色背景上是白色的,如上所示。只有Light主题修复了这个问题。 – Natix

-1

的解决方案是使用ActionBar的背景下,以创建AlertDialog:

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

或者使用ActionBarSherlock/ActionBarCompat:

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

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

然后使用该主题的环境中创建的AlertDialog

+1

这将使alertDialog使用darkTheme,而不是lightTheme – henry74918

+0

不适合我,没有在我的Android 4.4.4, Cyanogenmod –

+0

@ henry74918这会让你的对话与你的活动主题保持一致。如果你想要自定义/不同的你应该阅读其他答案,强制一个主题。 – Murphy

2

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

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

这不起作用,图标仍然隐藏。测试Nexus 7 4.4.2。 –

+0

已验证,不适用于我的4.4.4 CyanogenMod。 –

2

我面临同样的问题。 我使用从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级。

+0

没有帮助。图标仍然隐藏。 OS 4.1.2 – netimen

2

问题:

应用主题从机器人继承: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> 

注:这使得魔术最重要的行是<item name="android:textColor">@android:color/holo_blue_light</item>

2.用专用的主题建立一个AlertDialog时。

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

请看看之前和应用主题后,显示的屏幕截图。

screenshot

+0

我可以确认这适用于4.4.2。 – cgogolin

0

我试过上述解决方案,只为我工作(我的应用程序针对API 7,具有程序兼容性21.0.3)是该对话框样式设置为R.style.Theme_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); 
1

添加到您的主题:

<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

0

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