2

我想改变timepicker对话框片段的默认颜色。 现在我不知道我应该做什么。如何更改默认颜色方案timepicker对话框片段?

这是我设法我的主题设置为(withouth的的动作条):

enter image description here

但这是对话框我得到: enter image description here

这是我的XML样式文件:

<?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片段:

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"); 

请帮助我。 在此先感谢:)

+0

可能重复【如何改变的DatePicker和TimePicker对话框中Android的默认颜色?](http://stackoverflow.com/questions/11077530/how-to-change-the-default-color-of-datepicker-and-timepicker-dialog-in-android) –

回答

3

它花了一段时间,但我想通了! 在您的时间设置自定义主题选取器

转到styles.xml页面。 添加一个新的主题:

<style name="Dialog" parent="Base.Theme.AppCompat.Dialog"> 
     <itemname="android:timePickerDialogTheme">@style/AppTheme2</item> 
</style> 

定义继承乌尔自定义应用程序的主题(保持对应用程序一致性)一个主题,这里的timepicker对话的性质可以提到:

<style name="AppTheme2" parent="AppTheme.Base"> 
     <item name="android:windowIsFloating">true</item> 
</style> 

对我来说,这是我使用的整个应用程序的自定义主题:

<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> 

应用主题在呼吁时间选择器

TimePickerDialog dialog= new TimePickerDialog(
new ContextThemeWrapper(getActivity(),R.style.Dialog) 
, this, hour, minute 
, DateFormat.is24HourFormat(getActivity())); 

从我猜测,如果你想了定制,你应该在风格AppTheme2其添加(或任何ü要叫它)

这里是你可以添加到它的自定义列表(虽然不知道一切):

enter image description here

我希望这有助于。我在这里不是专家,但是这似乎工作,以便即时与它去;)

1

您可以指定TimerPickerDialog直接在构造函数中的主题资源,而不需要在应用程序/活动的主题使用ContextWrapperandroid:timePickerDialogTheme的(它只能从API 21中获得)。对于TimePickerDialog主题

例子:

<style name="TimePicker" parent="Theme.AppCompat.Light.Dialog"> 
    <item name="android:windowIsFloating">true</item> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
</style> 

传递其资源id作为第二个参数:

TimePickerDialog dialog = new TimePickerDialog(getActivity(), R.style.TimePicker, 
this, hour, minute, false);
相关问题