如何更改默认的配色方案时间选择器对话框片段? [英] How to change the default color scheme timepicker dialog fragment?
问题描述
我正在尝试更改时间选择器对话框片段的默认颜色. 现在我不知道我该怎么做.
I am trying to change the default color the timepicker dialog fragment. and right now i have no idea as to what im supposed to be doing.
这是我设法将主题设置为(没有actionBar的):
this what i have managed to set my theme to (withouth the actionBar):
但这是我得到的对话框:
but this is the dialog i get:
这是我的xml样式文件:
this is my xml style file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/color_accent</item>
<item name="colorPrimaryDark">@color/color_secondary</item>
<item name="colorAccent">@color/color_accent</item>
<item name="android:statusBarColor">@color/color_primary</item>
</style>
</resources>
我的颜色文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="flat_text">#f14268</color>
<color name="background_flatbtn">#fff</color>
<color name="color_accent">#f14268</color>
<color name="color_primary">#d83b5d</color>
<color name="color_secondary">#d83b5d</color>
</resources>
mytimepicker片段:
mytimepicker fragment:
public class DialogHandler extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
Context context;
public DialogHandler(Context context){
this.context=context;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
TimePickerDialog dialog= new TimePickerDialog(new ContextThemeWrapper(getActivity(),R.style.AppTheme), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
// Create a new instance of TimePickerDialog and return it
return dialog;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
Toast.makeText(context, "your time is selected",Toast.LENGTH_LONG).show();
}
}
我的对话电话:
DialogHandler newFragment = new DialogHandler(getApplicationContext());
newFragment.show(getSupportFragmentManager() , "time_Picker");
请帮助我. 在此先感谢:)
Please help me . Thanks in Advance :)
推荐答案
花了一段时间,但我发现了! 在您的时间选择器中设置自定义主题
It took a while but i figured it out! to setup the custom theme in your time Picker
转到styles.xml页面. 添加新主题:
go to the styles.xml page. add a new theme:
<style name="Dialog" parent="Base.Theme.AppCompat.Dialog">
<itemname="android:timePickerDialogTheme">@style/AppTheme2</item>
</style>
定义一个继承自定义应用程序主题的主题(以保持应用程序的一致性),在这里可以提及timepicker对话框的属性:
define a theme that inherits ur custom app theme(to maintain uniformity over the app), here the properties of the timepicker dialog can be mentioned :
<style name="AppTheme2" parent="AppTheme.Base">
<item name="android:windowIsFloating">true</item>
</style>
对我来说,这是我为整个应用程序使用的自定义主题:
for me this was the custom theme i was using for the whole app:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
在时间选择器中应用主题
apply theme in the call for time picker
TimePickerDialog dialog= new TimePickerDialog(
new ContextThemeWrapper(getActivity(),R.style.Dialog)
, this, hour, minute
, DateFormat.is24HourFormat(getActivity()));
根据我的猜测,如果您想进一步进行自定义,则应将其添加为AppTheme2样式(或您要调用的任何样式)
From my guess if you want anymore customization you should add it in the style AppTheme2(or whatever u want to call it)
这是您可以添加到其中的自定义列表(尽管不确定所有内容):
here is a list of customizations that u can add to it(not sure about everything though):
我希望这会有所帮助.我不是这里的专家,但这似乎可以正常工作;)
I hope this helps. I'm no expert here but this seems to work so im going with it ;)
这篇关于如何更改默认的配色方案时间选择器对话框片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!