AlertDialog主题:如何更改项目文本颜色?

29

当我尝试将标准主题应用于AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....

结果:

图片描述

需要注意的是,builder.setItems(...) 的文本颜色为黑色,而使用 builder.setSingleChoiceItems(...) 应用的主题具有白色文本颜色。

有什么快速修复方法吗?或者有没有办法基于 AlertDialog.THEME_DEVICE_DEFAULT_LIGHT 创建自定义主题?

我的自定义样式不如预期地工作:

<style name="AlertDialogCustomTheme" android:parent="android:Theme.Dialog">
    <item name="android:textColor">#7ABDFF</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>


    <!--THE FOLLOWING ITEMS HAVE NOT EFFECT ... !! -->

    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textColorAlertDialogListItem">#A844BD</item>
    <item name="android:itemBackground">#7ABDFF</item>
</style>

更新

@lopez的答案是完整的解决方案,但我找到了一个仅需一行修复我的问题的方法,在清单文件中应用自定义主题样式到该活动:

<style name="MyTheme">
    <item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>

很遗憾,该属性仅适用于API 11及以上的版本。 - jiduvah
5个回答

65

如果有人阅读这篇文章,并且在使用Support库的AlertDialogs时想要改变列表项的文本颜色,则需要去掉android:部分,就像这样:

<item name="textColorAlertDialogListItem">@color/your_color</item>

7
为未选中的单选按钮上色,请使用以下代码:<item name="android:textColorSecondary">@color/your_color</item>。 - Aditya Naique
2
如果有人像我一样遇到了困难,请尝试在项目名称前添加“android:”前缀...突然就可以工作了。 - koperko
1
你好!有可能让它在 parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog" 下工作吗?在我的情况下它不起作用。谢谢! - Mark Delphi
@MarkDelphi 我也遇到了同样的问题。你找到改变颜色的解决方案了吗? - Ravikiran
@Ravikiran 你好。很遗憾,还没有这个功能。我打算尝试使用自定义布局。 - Mark Delphi
显示剩余2条评论

5

个人而言,我使用Android的 Dialog,但我会使用自定义布局来匹配我的应用程序设计。

以下是一个例子:

new AlertDialog.Builder(context)
.setView(inflater.inflate(R.layout.dialog_delete_contact, null))
.setPositiveButton(context.getResources().getString(android.R.string.ok).toUpperCase(), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        // YOUR TREATMENT
    }
})
.setNegativeButton(context.getResources().getString(android.R.string.cancel).toUpperCase(), null)
.show();

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/GrayLight"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/Black"
    android:gravity="center"
    android:orientation="horizontal"
    tools:ignore="DisableBaselineAlignment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true"
            android:padding="10dip"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remove_contact"
            android:textColor="@color/White"
            android:textSize="20sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    tools:ignore="DisableBaselineAlignment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp"
        android:text="@string/ask_remove_contact"
        android:textSize="15sp"
        tools:ignore="HardcodedText" />

</LinearLayout>

图片结果如下:

enter image description here

为了避免每次重写代码,这里提供了一个实用类:

public class MyDialog {

public static Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
    View view = layoutInflater.inflate(R.layout.generic_dialog, null);
    ((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
    ((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
    return new AlertDialog.Builder(context).setView(view);
}

}

以下是一个使用示例:

AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), "Quitter ECOLEMS", "Voulez-vous vraiment quitter l'application?");
myDialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        // YOUR TREATMENT
    }
})
.setNegativeButton("Non", null)
.show();

我希望你可以提供帮助!

4

对我有效的方法

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@color/text</item>
    <item name="android:background">@color/background</item>
    <item name="android:textColorPrimary">@color/text</item>
    <item name="textColorAlertDialogListItem">@color/text</item>
</style>

0

这是对于我的对话框上的每种颜色都有效的方法:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#00ffff</item>
    <!-- Used for the title and text -->
    <item name="android:textColor">#00ff00</item>
    <item name="android:textColorAlertDialogListItem">#ffff00</item>
    <item name="android:textColorSecondary">#ff00ff</item>
    <!-- Used for the background -->
    <item name="android:background">#3a3a3c</item>
</style>

Every color of my alert dialog.


-4
AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....**change this line to**

builder.setSingleChoiceItems(info,0,null);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接