Android - 以编程方式在带有列表的 AlertDialog 上设置 TextColor ListItem
Android - Set TextColor ListItem on an AlertDialog with list programatically
我有一个 AlertDialog,用户必须在其中选择几个选项。默认的配色方案是深灰色背景上的白色文本。在应用程序中,用户可以配置自己的颜色,假设与白色相匹配。将背景更改为白色不是问题,但我无法设置项目的文本颜色。
我一直在寻找类似 dialog.getItems.setTextColor([usercolor]);
的东西,但我还没有找到解决方案。在我构建对话框的代码下方。
tvRekening.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(a);
builder.setTitle("Title")
.setItems(values, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
setRekeningView(which);
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(a, R.color.white)));
dialog.show();
}
});
进一步说明问题:
默认配色方案:
http://i.stack.imgur.com/nMKAA.png
将背景颜色更改为白色会使文本不可读(因为它仍然是白色)。
http://i.stack.imgur.com/PEABc.png
编辑:
我知道我可以为此创建一个自定义适配器。但是创建一个仅用于编辑项目文本颜色的适配器似乎有点矫枉过正。如果有人知道解决方案而无需经历创建适配器的麻烦,我们将不胜感激。如果没有,我会在一段时间内关闭问题。
您需要像这样创建自定义对话框:
Dialog d =new Dialog(context);
d.setContentView(R.layout.dialog);
ListView l=((ListView )d.findViewById(R.id.list));
看到这个linkAndroid - Executing a custom listview in a custom dialog properly
在 styles.xml 中创建新主题
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/alertDialogDark</item>
<item name="android:textColor">@android:color/white</item>
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorAlertDialogListItem">@android:color/white</item>
</style>
你需要把android:textColorAlertDialogListItem改成你喜欢的颜色
并在代码中写下这个
AlertDialog.Builder orderByBuilder = new AlertDialog.Builder(MainActivity.this, R.style.MyDialogTheme);
我有一个 AlertDialog,用户必须在其中选择几个选项。默认的配色方案是深灰色背景上的白色文本。在应用程序中,用户可以配置自己的颜色,假设与白色相匹配。将背景更改为白色不是问题,但我无法设置项目的文本颜色。
我一直在寻找类似 dialog.getItems.setTextColor([usercolor]);
的东西,但我还没有找到解决方案。在我构建对话框的代码下方。
tvRekening.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(a);
builder.setTitle("Title")
.setItems(values, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
setRekeningView(which);
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(a, R.color.white)));
dialog.show();
}
});
进一步说明问题:
默认配色方案: http://i.stack.imgur.com/nMKAA.png
将背景颜色更改为白色会使文本不可读(因为它仍然是白色)。 http://i.stack.imgur.com/PEABc.png
编辑: 我知道我可以为此创建一个自定义适配器。但是创建一个仅用于编辑项目文本颜色的适配器似乎有点矫枉过正。如果有人知道解决方案而无需经历创建适配器的麻烦,我们将不胜感激。如果没有,我会在一段时间内关闭问题。
您需要像这样创建自定义对话框:
Dialog d =new Dialog(context);
d.setContentView(R.layout.dialog);
ListView l=((ListView )d.findViewById(R.id.list));
看到这个linkAndroid - Executing a custom listview in a custom dialog properly
在 styles.xml 中创建新主题
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/alertDialogDark</item>
<item name="android:textColor">@android:color/white</item>
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorAlertDialogListItem">@android:color/white</item>
</style>
你需要把android:textColorAlertDialogListItem改成你喜欢的颜色
并在代码中写下这个
AlertDialog.Builder orderByBuilder = new AlertDialog.Builder(MainActivity.this, R.style.MyDialogTheme);