我想更改Android中日期/时间选择器对话框的默认颜色,以便它应该与我应用的主题相匹配。我在Google上搜索了解决方案,但我无法找到解决方案。
我正在做的是创造一种新风格:
<style name="datepicker" parent="@android:style/Widget.DeviceDefault.DatePicker">
<!---TODO-->
<item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>
不知道日期选择器对话的可用属性是什么。如果有人可以在
上发布链接,那就太好了在添加样式后,我将其作为主要样式调用
<item name="android:datePickerStyle">@style/datepicker</item>
不幸的是,这对我来说根本不起作用。
答案 0 :(得分:59)
像这样打电话
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DialogFragment dialogfragment = new DatePickerDialogTheme();
dialogfragment.show(getFragmentManager(), "Theme");
}
});
public static class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
//for one
//for two
DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,day);
//for three
DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);
// for four
DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_DARK,this,year,month,day);
//for five
DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);
//for six
DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_TRADITIONAL,this,year,month,day);
return datepickerdialog;
}
public void onDateSet(DatePicker view, int year, int month, int day){
TextView textview = (TextView)getActivity().findViewById(R.id.textView1);
textview.setText(day + ":" + (month+1) + ":" + year);
}
}
按照此操作,它将为您提供所有类型的日期选择器样式(从此复制)
http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/
答案 1 :(得分:51)
试试这个。这是最简单的&amp;最有效的方式
<style name="datepicker" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/primary</item>
</style>
答案 2 :(得分:21)
要在应用程序级别更改DatePicker
颜色(日历模式),请定义以下属性。
<style name="MyAppTheme" parent="Theme.AppCompat.Light">
<item name="colorAccent">#ff6d00</item>
<item name="colorControlActivated">#33691e</item>
<item name="android:selectableItemBackgroundBorderless">@color/colorPrimaryDark</item>
<item name="colorControlHighlight">#d50000</item>
</style>
有关其他DatePicker
自定义样式
答案 3 :(得分:9)
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.DatePickerDialogTheme, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = simpleDateFormat.format(newDate.getTime());
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
并使用这种风格:
<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/colorPrimary</item>
</style>
答案 4 :(得分:2)
不推荐使用AlertDialog.THEME
属性,因此在创建DatePickerDialog
时,您应该为int themeResId
传递这些参数之一
答案 5 :(得分:2)
创建自定义 DatePickerDialog
样式:
<style name="AppTheme.DatePickerDialog" parent="Theme.MaterialComponents.Light.Dialog">
<item name="android:colorAccent">@color/colorPrimary</item>
<item name="android:colorControlActivated">@color/colorPrimaryDark</item>
<item name="android:buttonBarPositiveButtonStyle">@style/AppTheme.Alert.Button.Positive</item>
<item name="android:buttonBarNegativeButtonStyle">@style/AppTheme.Alert.Button.Negative</item>
<item name="android:buttonBarNeutralButtonStyle">@style/AppTheme.Alert.Button.Neutral</item>
</style>
<style name="AppTheme.Alert.Button.Positive" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/buttonPositive</item>
</style>
<style name="AppTheme.Alert.Button.Negative" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/buttonNegative</item>
</style>
<style name="AppTheme.Alert.Button.Neutral" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/buttonNeutral</item>
</style>
在应用主题中设置自定义 datePickerDialogTheme
样式:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:datePickerDialogTheme">@style/AppTheme.DatePickerDialog</item>
</style>
在初始化时以编程方式设置主题,如下所示:
val datetime = DatePickerDialog(this, R.style.AppTheme_DatePickerDialog)
答案 6 :(得分:1)
(我正在使用React Native; targetSdkVersion 22)。我正在尝试更改日历日期选择器对话框的外观。可接受的答案对我不起作用,但确实可以。希望此摘要对您有所帮助。
<style name="CalendarDatePickerDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#6bf442</item>
<item name="android:textColorPrimary">#6bf442</item>
</style>
答案 7 :(得分:0)
创建新样式
<style name="my_dialog_theme" parent="ThemeOverlay.AppCompat.Dialog">
<item name="colorAccent">@color/colorAccent</item> <!--header background-->
<item name="android:windowBackground">@color/colorPrimary</item> <!--calendar background-->
<item name="android:colorControlActivated">@color/colorAccent</item> <!--selected day-->
<item name="android:textColorPrimary">@color/colorPrimaryText</item> <!--days of the month-->
<item name="android:textColorSecondary">@color/colorAccent</item> <!--days of the week-->
</style>
然后初始化对话框
Calendar mCalendar = new GregorianCalendar();
mCalendar.setTime(new Date());
new DatePickerDialog(mContext, R.style.my_dialog_theme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//do something with the date
}
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)).show();