Lollipop 中的 DialogFragment 按钮颜色变化
DialogFragment buttons color change in Lollipop
我希望我的 Fragments 看起来与我应用的应用程序和调色板的其余部分保持一致,因此我不仅想更改标题的颜色,还想更改 positive/negative 按钮的颜色:
我试过这样做,但不幸的是它不起作用:
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 文件在整个应用程序中执行此操作?
如果您可以使用 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);
}
}
要更改警报对话框中显示的按钮和复选框的颜色,您可以编辑:
values-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>
这将使对话框的按钮文本和复选框具有选定的颜色:
values-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:
前缀
我希望我的 Fragments 看起来与我应用的应用程序和调色板的其余部分保持一致,因此我不仅想更改标题的颜色,还想更改 positive/negative 按钮的颜色:
我试过这样做,但不幸的是它不起作用:
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 文件在整个应用程序中执行此操作?
如果您可以使用 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);
}
}
要更改警报对话框中显示的按钮和复选框的颜色,您可以编辑:
values-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>
这将使对话框的按钮文本和复选框具有选定的颜色:
values-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:
前缀