我希望我的片段看起来与我应用的其他应用程序和调色板一致,所以我想更改标题的颜色,还要更改正/负按钮的颜色:
我试图这样做,但不幸的是它不起作用:
public void onStart() {
super.onStart();
Dialog d = getDialog();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
if(currentapiVersion< Build.VERSION_CODES.LOLLIPOP) {
divider.setBackgroundColor(getResources().getColor(R.color.accent));
LinearLayout ll = (LinearLayout) d.findViewById(R.id.dialog_background);
ll.setBackgroundResource(R.drawable.backrepeat_reversed);
}
if(currentapiVersion == Build.VERSION_CODES.LOLLIPOP) {
int buttonsId = d.getContext().getResources().getIdentifier("android:id/negativeButton", null, null);
Button b = (Button) d.findViewById(buttonsId);
b.setTextColor(getResources().getColor(R.color.accent));
}
int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.accent));
}
如何更改这些按钮的颜色?也许可以通过styles.xml文件在整个应用程序中完成它?
答案 0 :(得分:34)
如果您可以使用AlertDialog
创建对话框,则以下内容适用于我:
public class DialogTest extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle("Test")
.setMessage("This is a dialog.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
@Override
public void onStart() {
super.onStart();
((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
}
}
答案 1 :(得分:9)
要更改“警报”对话框中显示的按钮和复选框的颜色,您可以编辑:
值-21 / styles.xml
<style name="AppTheme" parent="...">
...
<item name="android:timePickerDialogTheme">@style/AlertDialogCustom</item>
<item name="android:datePickerDialogTheme">@style/AlertDialogCustom</item>
<item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
</style>
<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
<item name="android:colorPrimary">#00397F</item>
<item name="android:colorAccent">#0AAEEF</item>
</style>
这将使对话框具有按钮文本和复选框的选定颜色:
答案 2 :(得分:1)
值-21 / styles.xml
<style name="AppTheme" parent="...">
...
<item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/buttons_color</item>
</style>
在Lollipop及以上版本中,您不应使用android:
前缀