13

Chcę zmienić domyślny kolor okna wyboru daty/czasu w systemie Android, tak aby pasował do motywu mojej aplikacji. Szukałem rozwiązania w Google, ale nie mogłem znaleźć rozwiązania.Jak zmienić styl DatePicker w Androidzie?

Co robiłem było stworzenie nowego stylu:

<style name="datepicker" parent="@android:style/Widget.DeviceDefault.DatePicker"> 
    <!---TODO--> 
    <item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item> 
</style> 

Nie wiem jakie są atrybuty dostępne dla dialogu wyboru daty. Byłoby wspaniale, gdyby każdy mógł umieścić link na tej

i po dodaniu styl Byłem wzywającą go w moim głównym stylu jak

<item name="android:datePickerStyle">@style/datepicker</item> 

niestety to nie działa dla mnie w ogóle.

Odpowiedz

31

Spróbuj tego. To najprostszy & najskuteczniejszym sposobem

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

@ShirishHerwade Nie ma żadnego powiązania. Uprzejmie krótko zapytać – silverFoxA

32

wywołanie jak to

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

    } 
    } 

śledzić ten to daje cały styl data typ kompletacji (przepisać z tego)

http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/

enter image description here

enter image description here

enter image description here

+2

4-5 nie działa na Android 7 – WolfJee

+3

@WolfJee używać w ten sposób; int theme; if (Build.VERSION.SDK_INT <23) theme = AlertDialog.THEME_HOLO_DARK; else theme = android.R.style.Theme_Holo_Dialog; new DatePickerDialog (getActivity(), motyw, data, ...... – kaya

+0

świetna robota, golisz mój czas – Ninja

3
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(); 

I wykorzystać ten styl

<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog"> 
     <item name="colorAccent">@color/colorPrimary</item> 
</style> 
+0

Działa dla mnie –

8

Aby zmienić kolory datepicker (tryb kalendarz) na poziomie aplikacji zdefiniować poniżej właściwości.

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

Zobacz http://www.zoftino.com/android-datepicker-example innych datepicker niestandardowych stylów

zoftino DatePicker examples

Powiązane problemy