Android-从DatePickerDialog更改线条颜色

时间:2018-07-26 18:53:30

标签: java android datepicker themes

我将Theme.Holo.Light.Dialog.NoActionBar.MinWidth用于DatePickerDialog,但我想更改蓝色,以便将其变为红色。

我成功更改了文本的颜色,但是我没有找到如何更改线条的颜色。

enter image description here

我尝试了很多事情,这是当前的代码:

<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog.NoActionBar.MinWidth">
        <item name="colorAccent">@color/smoothRed</item>
        <item name="colorPrimary">@color/smoothRed</item>
        <item name="android:textColor">@color/smoothRed</item>
        <item name="textColor">@color/smoothRed</item>
        <item name="android:background">@null</item>
        <item name="android:textColorPrimary">@color/colorPrimary</item>
    </style>

1 个答案:

答案 0 :(得分:1)

您可以尝试此解决方案。为我工作。

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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


    DatePickerDialog datepickerdialog = new DatePickerDialog(this,
            AlertDialog.THEME_HOLO_DARK,this,year,month,day);

    colorizeDatePicker(datepickerdialog.getDatePicker());

    datepickerdialog.show();


}


    public static void colorizeDatePicker(DatePicker datePicker) {
        Resources system = Resources.getSystem();
        int dayId = system.getIdentifier("day", "id", "android");
        int monthId = system.getIdentifier("month", "id", "android");
        int yearId = system.getIdentifier("year", "id", "android");

        NumberPicker dayPicker = (NumberPicker) datePicker.findViewById(dayId);
        NumberPicker monthPicker = (NumberPicker) datePicker.findViewById(monthId);
        NumberPicker yearPicker = (NumberPicker) datePicker.findViewById(yearId);

        setDividerColor(dayPicker);
        setDividerColor(monthPicker);
        setDividerColor(yearPicker);
    }

    private static void setDividerColor(NumberPicker picker) {
        if (picker == null)
            return;

        final int count = picker.getChildCount();
        for (int i = 0; i < count; i++) {
            try {
                Field dividerField = picker.getClass().getDeclaredField("mSelectionDivider");
                dividerField.setAccessible(true);
                ColorDrawable colorDrawable = new ColorDrawable(picker.getResources().getColor(R.color.colorAccent));
                dividerField.set(picker, colorDrawable);
                picker.invalidate();
            } catch (Exception e) {
                Log.w("setDividerColor", e);
            }
        }

}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

}
}