更改TimePickerDialog的主题以使用AppTheme

时间:2016-01-11 03:17:55

标签: android

我使用developer.android.com中提供的以下代码实现了TimePickerDialog

public static class TimePickerFragment extends DialogFragment
                        implements TimePickerDialog.OnTimeSetListener {

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

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}

}

我得到一个像这样的对话框:

enter image description here

我想更改TimePickerDialog的主题以使用扩展AppTheme主题的AppCompat。当我使用其他构造函数时,一些人说,主题可以工作,但Dialog全屏。我用的代码是:

// Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), R.style.AppTheme, this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));

我明白了:

enter image description here

我该如何解决这个问题? - 我希望Dialog保持不变,但只改变强调色。谢谢!

3 个答案:

答案 0 :(得分:16)

您必须使用Theme.AppCompat.Light.Dialog作为Parent Theme

您需要在Dialog中定义styles.xml主题。

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">@color/purple</item>
        <item name="colorControlNormal">@color/orange</item>
 </style>

然后像这样使用它:

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), R.style.DialogTheme, this, hour, minute,
DateFormat.is24HourFormat(getActivity()));

我希望它可以帮到你。

答案 1 :(得分:0)

我们可以更改主题,如下例所示。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    // Get a Calendar instance
    final Calendar calendar = Calendar.getInstance();
    // Get the current hour and minute
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);

    /*
        Creates a new time picker dialog with the specified theme.

            TimePickerDialog(Context context, int themeResId,
                TimePickerDialog.OnTimeSetListener listener,
                int hourOfDay, int minute, boolean is24HourView)
     */

    // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT
    TimePickerDialog tpd = new TimePickerDialog(getActivity(),
            AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_DARK
    TimePickerDialog tpd2 = new TimePickerDialog(getActivity(),
            AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_HOLO_DARK
    TimePickerDialog tpd3 = new TimePickerDialog(getActivity(),
            AlertDialog.THEME_HOLO_DARK,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_HOLO_LIGHT
    TimePickerDialog tpd4 = new TimePickerDialog(getActivity(),
            AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_TRADITIONAL
    TimePickerDialog tpd5 = new TimePickerDialog(getActivity(),
            AlertDialog.THEME_TRADITIONAL,this,hour,minute,false);

    // Return the TimePickerDialog
    return tpd;
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute){
    // Do something with the returned time
    TextView tv = (TextView) getActivity().findViewById(R.id.tv);
    tv.setText("HH:MM\n" + hourOfDay + ":" + minute);
}

}

有关更详细的示例,请参阅此tutorial

我希望这会对你有所帮助。

答案 2 :(得分:0)

您也可以像这样覆盖 getTheme 方法:

public class TimePickerFragment extends DialogFragment {
    
    ...
  
    @Override
    public int getTheme() {
        return R.style.MyDialogTheme;
    }

    ...
}