Android AlertDialog主题:如何更改项目文本颜色?
当我尝试将标准主题应用于Android AlertDialog主题:如何更改项目文本颜色?,android,themes,android-alertdialog,Android,Themes,Android Alertdialog,当我尝试将标准主题应用于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 arrayAda
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(…)
没有问题,因为它的文本颜色是Black
,而与builder.setSingleChoiceItems(…)
一起应用的主题是白色文本颜色
有什么快速修复方法吗?或者基于AlertDialog.theme\u DEVICE\u DEFAULT\u 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>
#7ABDFF
真的
@android:彩色/透明
真的
真的
#A844BD
#7ABDFF
更新
@lopez answer是一个完整的解决方案,但我为我的问题找到了一个单行修复程序,一个应用于清单中活动的自定义主题:
<style name="MyTheme">
<item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>
@android:彩色/黑色
就我个人而言,我使用安卓对话框
,但我使用与我的应用程序设计相匹配的自定义布局
以下是一个例子:
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>
以及使用以下各项的示例:
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();
我希望你能帮助我 如果有人碰巧读到这篇文章,并且正在使用支持库警报对话框,并且想要更改列表项的文本颜色,那么请删除android:部分,如下所示:
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);
<item name="textColorAlertDialogListItem">@color/your_color</item>
@color/你的颜色
什么适合我
<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>
@颜色/文字
@颜色/背景
@颜色/文字
@颜色/文字
这是我在对话框中对每种颜色都有效的方法:
<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>
#00ffff
#00ff00
#ffff00
#ff00ff
#3A3C
不幸的是,该属性仅在API 11及以上版本中可用,用于为未选中的单选按钮上色
@color/your_color
如果有人像我一样被困在这里,请尝试在项目名称中添加android:
前缀。。突然开始工作了!嗨!是否有可能使其适用于parent=“ThemeOverlay.MaterialComponents.MaterialAlertDialog”
?就我而言,它不起作用。谢谢@我也有同样的问题。你找到改变现状的办法了吗color@Ravikiran你好不幸的是,还没有。我将尝试使用自定义布局。
<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>