如何改变android中DatePicker的样式?
How to change the style of a DatePicker in android?
我想更改 Android 中 date/time 选择器对话框的默认颜色,使其与我的应用程序主题相匹配。我在 Google 上搜索了解决方案,但找不到解决方案。
我正在做的是创造一种新风格:
<style name="datepicker" parent="@android:style/Widget.DeviceDefault.DatePicker">
<!---TODO-->
<item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>
不知道日期选择器对话有哪些可用属性。如果有人能 post 一个 link 就太好了
添加样式后,我在主样式中称它为
<item name="android:datePickerStyle">@style/datepicker</item>
不幸的是,这对我根本不起作用。
试试这个。这是最简单有效的方式
<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>
像这样打电话
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/
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>
要在应用程序级别更改 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
自定义样式,请参阅 http://www.zoftino.com/android-datepicker-example
(我正在使用 React Native;targetSdkVersion 22)。我正在尝试更改日历日期选择器对话框的外观。接受的答案对我不起作用,但这确实有效。希望这个片段对你们中的一些人有所帮助。
<style name="CalendarDatePickerDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#6bf442</item>
<item name="android:textColorPrimary">#6bf442</item>
</style>
由于 AlertDialog.THEME
属性已弃用,在创建 DatePickerDialog
时,您应该为 int themeResId
传递这些参数之一
- android.R.style.Theme_DeviceDefault_Dialog_Alert
- android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
- android.R.style.Theme_Material_Light_Dialog_Alert
- android.R.style.Theme_Material_Dialog_Alert
创建新样式
<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();
结果:
创建自定义 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)
如果您在 XML 中使用 DatePicker,您所要做的就是为您的对话框添加一个样式,就像 @theBeardedPilot 提到的那样:
<style name="DialogTheme" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">@android:color/holo_green_dark</item>
<!--selected day-->
<item name="android:colorControlActivated">@android:color/holo_red_dark</item>
<!--days of the month-->
<item name="android:textColorPrimary">@android:color/holo_orange_dark</item>
<!--days of the week-->
<item name="android:textColorSecondary">@android:color/holo_blue_dark</item>
</style>
然后使用 android:theme 属性调用此样式,如下所示:
<DatePicker
android:theme="@style/DialogTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
我想更改 Android 中 date/time 选择器对话框的默认颜色,使其与我的应用程序主题相匹配。我在 Google 上搜索了解决方案,但找不到解决方案。
我正在做的是创造一种新风格:
<style name="datepicker" parent="@android:style/Widget.DeviceDefault.DatePicker">
<!---TODO-->
<item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>
不知道日期选择器对话有哪些可用属性。如果有人能 post 一个 link 就太好了
添加样式后,我在主样式中称它为
<item name="android:datePickerStyle">@style/datepicker</item>
不幸的是,这对我根本不起作用。
试试这个。这是最简单有效的方式
<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>
像这样打电话
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/
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>
要在应用程序级别更改 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
自定义样式,请参阅 http://www.zoftino.com/android-datepicker-example
(我正在使用 React Native;targetSdkVersion 22)。我正在尝试更改日历日期选择器对话框的外观。接受的答案对我不起作用,但这确实有效。希望这个片段对你们中的一些人有所帮助。
<style name="CalendarDatePickerDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#6bf442</item>
<item name="android:textColorPrimary">#6bf442</item>
</style>
由于 AlertDialog.THEME
属性已弃用,在创建 DatePickerDialog
时,您应该为 int themeResId
- android.R.style.Theme_DeviceDefault_Dialog_Alert
- android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
- android.R.style.Theme_Material_Light_Dialog_Alert
- android.R.style.Theme_Material_Dialog_Alert
创建新样式
<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();
结果:
创建自定义 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)
如果您在 XML 中使用 DatePicker,您所要做的就是为您的对话框添加一个样式,就像 @theBeardedPilot 提到的那样:
<style name="DialogTheme" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">@android:color/holo_green_dark</item>
<!--selected day-->
<item name="android:colorControlActivated">@android:color/holo_red_dark</item>
<!--days of the month-->
<item name="android:textColorPrimary">@android:color/holo_orange_dark</item>
<!--days of the week-->
<item name="android:textColorSecondary">@android:color/holo_blue_dark</item>
</style>
然后使用 android:theme 属性调用此样式,如下所示:
<DatePicker
android:theme="@style/DialogTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />