在AlertDialog中更正文本颜色/外观

flx*_*pps 8 android android-layout android-alertdialog

我很长一段时间都在努力解决这个问题.我搜索了解决方案,实际上有一些相关问题的建议,但没有什么真正适合我.所以假设我想创建一个带有(长)消息,复选框和两个按钮的AlertDialog.

// 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();
Run Code Online (Sandbox Code Playgroud)

不起作用,因为如果对话框消息太长,则不会完全显示该复选框.此外,它将完全隐藏在横向模式中.

接下来尝试将查找原始对话框布局文件(对我而言,它位于android-sdk-linux_86/platforms/android-17/data/res/layout/alert_dialog.xml下)并尝试复制相关部分以创建我们的拥有内部对话框布局的"克隆",就像这样:

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>
Run Code Online (Sandbox Code Playgroud)

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

// 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();
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 19

我遇到了同样的问题并通过为我想要的Dialog Theme创建一个ContextThemeWrapper来解决它,并在创建AlertDialog Builder时以及在将View添加到附件时使用它:

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);
Run Code Online (Sandbox Code Playgroud)

样式R.style.DialogBaseTheme在姜饼的"/values/styles.xml"中定义,下面为:

<style name="DialogBaseTheme" parent="android:Theme.Dialog"/>
Run Code Online (Sandbox Code Playgroud)

然后它也在Honeycomb的"/values-v11/styles.xml"中定义,并向上使用Holo主题:

<style name="DialogBaseTheme" parent="android:Theme.Holo.Light.Dialog" />
Run Code Online (Sandbox Code Playgroud)

所以在Gingerbread上我的自定义View的TextViews自动着色为白色,而Honeycomb +它们会自动为Holo Light着色为黑色.

  • 我一直想知道如何做到这一点.这非常有效. (2认同)

And*_*ler 0

在textview中添加一个属性:

android:textColor="@color/white"
Run Code Online (Sandbox Code Playgroud)

并在 res/values 文件夹内的 color.xml 中定义白色

<color name="white">#ffffff</color>
Run Code Online (Sandbox Code Playgroud)

或者你也可以直接添加

android:textColor="#fffff"(虽然不推荐。推荐以上方法。)