Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

android - Change theme of TimePickerDialog to use AppTheme

I have implemented a TimePickerDialog using the following code as given in developer.android.com:

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
}

}

I get a dialog like this one:

enter image description here

I want to change the theme of the TimePickerDialog to go with the AppTheme which extends an AppCompat theme. When i do use the other constructor as told by some, the theme works, but the Dialog goes fullscreen. The code i used for that was:

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

I get this:

enter image description here

How do i go about this? - I want the Dialog to remain the same, but change only the accent colors. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use Theme.AppCompat.Light.Dialog as Parent Theme

You need to define Dialog theme in styles.xml.

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

then use it like this:

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

I hope it helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...