安卓:发行与显示来自Theme.Light活动对话框 [英] Android: Issue with showing dialog from Theme.Light activity
问题描述
我想表明从preferenceActivity,它被设置为Theme.Light的对话框。该对话框显示了在深色背景上深色的文字:
I am trying To show a dialog from a PreferenceActivity, which is set to Theme.Light. The dialog shows with dark text on a dark background:
我假定它使用深色文本,因为它继承了父活动,或类似的文本颜色。我想对话框,或者使用白色文字黑暗的背景,或者使用白色背景黑色文本,为preferenceActivity当设置为Theme.Light呢。
I assume it uses dark text because it is inheriting the text color from the parent activity, or something similar. I would like the dialog to either use white text on the dark background, or use a white background with dark text, as the PreferenceActivity does when set to Theme.Light.
这似乎是一个已知的问题,我已经找到了解决方法涉及到创建和使用,扩展Theme.Dialog自定义样式,并用它来实例化对话框。是这样的:
This seems to be a known problem, the workarounds I have found involve creating and using a custom style that extends Theme.Dialog and using it to instantiate the dialog. Something like:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
</style>
Dialog dialog = new Dialog(context, R.style.CustomDialog);
我想这一点,但它并没有区别。我也尝试了一些不同的值的文字颜色,其中没有修改对话框的文本颜色。作为一个全面的检查,我说:
I tried this, but it made no difference. I also tried a number of different values for textColor, none of which modified the Dialog's text color. As a sanity check, I added:
<item name="android:background">#FFFF0000</item>
的风格,这就造成了一个红色的背景对话框(所以我相信我正确实例化对话框)。
to the style, which resulted in a dialog with a red background (so I am sure that I am instantiating the dialog properly).
我来解决最近刚刚设置对话框的背景色为白色,这给下面的对话框。但是,这不是一个很好的解决方案,因为有些版本或某些设备可能不会使用相同的行为,我倒相文本颜色时看到:
The closest I have come to a solution is just setting the dialog's background color to white, which gives the below dialog. But this is not a good solution, because some version or some device might not use the same behavior I am seeing when inverting text color:
那么,有没有设置文本颜色上从Theme.Light活动显示一个对话框,一个好办法?
So, is there a good way to set text color on a dialog displayed from a Theme.Light activity?
推荐答案
我假设你使用 AlertDialog.Builder
,并设置使用<$ C $的一个列表C>不使用setSingleChoiceItems 方法,你自己的 ListAdapter
。相反,它会创建自己用,而不是错款式。为了解决这个问题,你应该叫 setSingleChoiceItems(ListAdapter适配器,诠释checkedItem,DialogInterface.OnClickListener听众)
和提供这样它将使用的布局与风格需要一个适配器。
I assume that you use AlertDialog.Builder
and set the list using one of the setSingleChoiceItems
methods which doesn't use your own ListAdapter
. Instead it creates its own instead with the wrong style. To fix this, you should call setSingleChoiceItems(ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)
and provide such an adapter which would use a layout with the needed style.
现在,为什么会这样。实际适配器创建发生在文件 com.android.internal.app.AlertController
,其中以下行选择单选项列表的布局:
Now, why this happens. The actual adapter creation happens in the file com.android.internal.app.AlertController
, where the following line selects the layout for single choice lists:
int layout = mIsSingleChoice
? R.layout.select_dialog_singlechoice : R.layout.select_dialog_item;
下面是前面提到的布局:
Here is the aforementioned layout:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/primary_text_light_disable_only"
android:gravity="center_vertical"
android:paddingLeft="12dip"
android:paddingRight="7dip"
android:checkMark="@android:drawable/btn_radio"
android:ellipsize="marquee"
/>
正如你所看到的,它设置文本颜色的行包含没有引用一个主题,但硬连线的颜色。这就是为什么当这件事列表创建过程中被夸大,它总是希望它使用什么样的风格使用相同的颜色,不管。因此,要解决这个问题的正确动作是用自己的布局和你自己的 ListAdapter
。
这篇关于安卓:发行与显示来自Theme.Light活动对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!