在AlertDialog中更正文本的颜色/外观 [英] Correct text color/appearance in AlertDialog

查看:330
本文介绍了在AlertDialog中更正文本的颜色/外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上苦苦挣扎了很长时间.我在寻找解决方案,实际上有一些有关相关问题的建议,但对我来说并没有什么真正起作用的.因此,假设我要创建一个带有(长)消息,一个复选框和两个按钮的AlertDialog.

I am struggling around with this problem for quite a long time. I searched for solutions and there were actually some suggestions for related problems, but nothing really worked correctly for me. So let's say I want to create an AlertDialog with a (long) message, a checkbox and two buttons.

// create dialog
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.dlg_title);
dlg.setMessage(R.string.dlg_msg);

// add checkbox
final CheckBox checkbox = new CheckBox(this);
dlg.setView(checkbox);

// add buttons
dlg.setPositiveButton(...);
dlg.setNegativeButton(...);

// show dialog
dlg.show();

无效,因为如果对话框消息过长,则该复选框将不会完全显示.此外,它将在横向模式下完全隐藏.

This won't work, because if the dialog message becomes too long, the checkbox will not be completely shown. Additionally, it is going to be completely hidden in landscape mode.

下一个尝试是查找

Next try would be to lookup the original dialog layout file (for me it is located under android-sdk-linux_86/platforms/android-17/data/res/layout/alert_dialog.xml) and attempt to copy the relevant part to create our own "clone" of the inner dialog layout, just like this:

dialog_checkbox.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:overScrollMode="ifContentScrolls"
        android:paddingBottom="12dip"
        android:paddingLeft="14dip"
        android:paddingRight="10dip"
        android:paddingTop="2dip" >

        <TextView
            android:id="@+id/message"
            style="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dip" />
    </ScrollView>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我们可以尝试将此视图添加到对话框中,如下所示:

We could try to add this view to our dialog like this:

// create dialog
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.dlg_title);

// add checkbox
LinearLayout checkboxLayout = (LinearLayout) View.inflate(mContext, R.layout.dialog_checkbox, null);
((TextView) checkboxLayout.findViewById(R.id.message)).setText(R.string.dlg_msg);
mCheckbox = (CheckBox) checkboxLayout.findViewById(R.id.checkbox);
mCheckbox.setText(R.string.dlg_checkbox_msg);
setView(checkboxLayout);

// add buttons
dlg.setPositiveButton(...);
dlg.setNegativeButton(...);

// show dialog
dlg.show();

这实际上很有效.几乎.在我们为应用程序使用Theme.Light之前,我们不会看到任何问题.但是,一旦我们使用Theme.Light,对话框的文本颜色将变成黑色,并且在深色背景上不可读. 为什么?!

  • 我们已经克隆了原始的Android警报对话框xml布局源代码.但是,尽管我们将attr/textAppearanceMedium用作文本样式,但在Theme.Light下,文本颜色仍然变为黑色.
  • 如果我们使用Theme.Light并通过setMessage创建带有普通消息的普通"对话框,则文本颜色为白色且可读.

怎么了?怎么解决呢?我不想以编程方式将文本颜色设置为白色或黑色,这在自定义用户主题上看起来不太好.有什么建议吗?

What's wrong? How to solve this? I don't want to programmatically set the text color to white or black, this will not look good on custom user themes. Any suggestions?

推荐答案

我遇到了同样的问题,并通过为所需的Dialog主题创建ContextThemeWrapper来解决了该问题,并在创建AlertDialog Builder和扩展View时使用了该主题:

I had the same problem and solved it by creating a ContextThemeWrapper for my desired Dialog Theme and used this when creating the AlertDialog Builder and when inflating the View to attach:

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.DialogBaseTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
View view = LayoutInflater.from(wrapper).inflate(R.layout.create_warning, null); 
builder.setView(view);

R.style.DialogBaseTheme样式在"/values/styles.xml"中为Gingerbread及以下版本定义:

The style R.style.DialogBaseTheme is defined in "/values/styles.xml" for Gingerbread and below as:

<style name="DialogBaseTheme" parent="android:Theme.Dialog"/>

然后在"/values-v11/styles.xml"中为Honeycomb及更高版本定义它,以将Holo主题用作:

Then it is also defined in "/values-v11/styles.xml" for Honeycomb and upward to use the Holo theme as:

<style name="DialogBaseTheme" parent="android:Theme.Holo.Light.Dialog" />

因此,在Gingerbread上,我的自定义视图的TextView会自动着色为白色,而Honeycomb +会自动为Holo Light着色为黑色.

So on Gingerbread my custom View's TextViews are automatically coloured white and Honeycomb+ they are automatically coloured black for Holo Light.

这篇关于在AlertDialog中更正文本的颜色/外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆