【问题标题】:Correct text color/appearance in AlertDialog在 AlertDialog 中更正文本颜色/外观
【发布时间】:2012-12-25 18:20:29
【问题描述】:

我在这个问题上苦苦挣扎了很长时间。我搜索了解决方案,实际上有一些针对相关问题的建议,但对我来说没有什么能真正正常工作。因此,假设我想创建一个带有(长)消息、一个复选框和两个按钮的 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();

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

下一个尝试是查找original dialog layout file(对我来说它位于 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>

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

// 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 创建一个带有普通消息的“普通”对话框,则文本颜色为白色且可读。

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

【问题讨论】:

    标签: android android-layout android-alertdialog


    【解决方案1】:

    我遇到了同样的问题,并通过为我想要的对话框主题创建一个 ContextThemeWrapper 来解决它,并在创建 AlertDialog Builder 和膨胀要附加的视图时使用它:

    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 定义如下:

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

    那么在“/values-v11/styles.xml”中也定义了Honeycomb及以上使用Holo主题为:

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

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

    【讨论】:

    • 非常感谢您的回答!完美运行!
    【解决方案2】:

    思考我终于找到了解决方法。根据我的初始代码,这似乎解决了我的问题:

    // add checkbox layout
    LinearLayout checkboxLayout = (LinearLayout) View.inflate(new ContextThemeWrapper(context, android.R.style.Theme_Dialog), R.layout.dialog_checkbox, null);
    ((TextView) checkboxLayout.findViewById(R.id.message)).setText(DIALOG_TEXT);
    mCheckbox = (CheckBox) checkboxLayout.findViewById(R.id.checkbox);
    mCheckbox.setText(CHECKBOX_NOT_AGAIN);
    setView(checkboxLayout);
    

    注意 ContextThemeWrapper。它使得对话框主题被分配给膨胀的布局。我希望,这最终解决了这个问题。

    【讨论】:

    • 不。似乎它不适用于Android 4 ......该死的。谁能找到合适的解决方案?
    【解决方案3】:

    在 textview 里面添加一个属性:

    android:textColor="@color/white"
    

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

    <color name="white">#ffffff</color>
    

    也可以直接添加

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

    【讨论】:

    • 如前所述,我不喜欢这种解决方案。什么,如果有人使用自定义主题,对话框有浅色背景和深色文本颜色?那时他们会遇到问题。在我看来,这个解决方案根本不干净。
    • 不,你可以试试这个,因为如果你明确地给 textcolor white ,它将覆盖任何主题的默认颜色。
    • 你不懂。一些 Android 用户有自定义 ROM,其中一些自定义 ROM 具有单独的主题。其中一些单独的主题可能具有非常鲜艳的色彩。其中一些色彩鲜艳的主题可能有明亮的对话背景。而那些具有明亮对话背景的主题将在逻辑上使用深色文本颜色来使对话文本可读。如果我用白色覆盖此文本颜色,则会在明亮的背景上显示白色文本。你能在白色背景上阅读白色文字吗?我不这么认为......
    • @user1684030 在这种情况下,您可以创建一个自定义 xml 文件并将其背景设置为黑色,并将 textview 颜色设置为白色。它可以正常工作。
    • 好吧,那么文本可能是可读的,但对用户来说仍然看起来很丑陋和不寻常。
    【解决方案4】:

    您可以应用带有阴影自定义样式,以使您的文本可见,无论文本颜色是什么:

    <resources>
    <style name="Text.Shaded.Medium" parent="@android:attr/textAppearanceMedium">
        <item name="@android:paddingLeft">4dp</item>
        <item name="@android:paddingBottom">4dp</item>
        <item name="@android:textColor">#FFFFFFFF</item>
        <item name="@android:shadowColor">#000000</item>
        <item name="@android:shadowDx">2</item>
        <item name="@android:shadowDy">2</item>
        <item name="@android:shadowRadius">2</item>
    </style>
    </resources>
    

    但是如果它对您来说不够明显,您可以应用内阴影,但不幸的是 Android 没有这样的属性,所以您必须使用类似 MagicTextView 的东西(ABentSpoon 在这个问题中提到:Is there a way to add inner shadow to a TextView on Android?)。

    【讨论】:

    • 这个解决方案也搞乱了默认的对话框外观。它使对话框外观对用户来说不可用和奇怪。我不喜欢这样的解决方案。
    猜你喜欢
    • 2023-03-22
    • 2019-04-11
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多