我已经阅读了几篇stackoverflow文章,但遗憾的是还没有找到对我有用的东西:我有一个使用时间选择器的TimePreference。我相信由于我的配色方案,这些数字没有显示出来(似乎是白色的):Screenshot of how it looks at the moment
我试图弄清楚如何通过一种样式影响数字颜色。运气不好,但是对于一些指向android:TimePickerStyle
的类似的栈溢出答案,它需要比我设置的API(API 15)高的API。
我也尝试过使用自定义布局,但是我不确定在哪里最好在TimePreference.class中调用布局(或者我发生在正确的位置,但是也没有运气)。
我还研究了更改表盘颜色的可能性,但也没有得到进一步的解决。
TimePreference当前如下所示(未实现自定义布局):
public class TimePreference extends DialogPreference {
private int lastHour = 0;
private int lastMinute = 0;
private boolean isAm;
private TimePicker picker = null;
private static int getHour(String time) {
String[] pieces = time.split(":");
return (Integer.parseInt(pieces[0]));
}
private static int getMinute(String time) {
String[] pieces = time.split(":");
return (Integer.parseInt(pieces[1]));
}
public TimePreference(Context ctxt, AttributeSet attrs) {
super(ctxt, attrs);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
@Override
protected View onCreateDialogView() {
picker = new TimePicker(getContext());
//check locale settings whether to use am/pm or 24 hour display
picker.setIs24HourView(false);
return (picker);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
lastHour = picker.getCurrentHour();
lastMinute = picker.getCurrentMinute();
String time = String.valueOf(lastHour) + ":" + String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return (a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time;
if (restoreValue) {
if (defaultValue == null) {
time = getPersistedString("12:00 pm");
} else {
time = getPersistedString(defaultValue.toString());
}
} else {
time = defaultValue.toString();
}
lastHour = getHour(time);
lastMinute = getMinute(time);
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
isAm = hourOfDay < 12;
}
};
}
public String getTime() {
String meridianId;
if (isAm) {
if (lastHour > 12) {
meridianId = " pm";
} else {
meridianId = " am";
}
} else {
meridianId = "";
}
return lastHour + ":" + lastMinute + meridianId;
}
}
我的style.xml目前看起来像这样:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
(…)
<item name="android:dialogTheme">@style/DialogTheme</item>
<item name="android:alertDialogTheme">@style/DialogTheme</item>
</style>
<style name="AppAlertDialogContent" parent="Theme.AppCompat.Dialog">
<!-- To tint EditText and TimePicker -->
<item name="colorAccent">@color/colorAccent</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@color/colorPrimaryDark</item>
<item name="android:windowBackground">@color/colorPrimary</item>
</style>
<style name="DialogTheme" parent="Theme.AppCompat.Dialog">
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@color/colorLightAccent</item>
<!-- Used for the background -->
<item name="android:background">@color/colorPrimary</item>
<!-- Used for the buttons -->
<item name="colorAccent">@color/colorLightAccent</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
非常感谢您在确保实际读取数字方面所提供的帮助。无论是通过自定义布局,样式还是其他一些漂亮的方法。谢谢您的帮助。
答案 0 :(得分:0)
深入研究Timepicker小部件后,我不得不承认在API 21下这是不可能的。因此,我最终使用两个Numberpickers和一个ToggleButton构建了自己的自定义UI。这给了我足够的可达视图,在这里我可以充分控制颜色以确保其可读性。因为android:theme
是救生员,因为这是我可以(最终!)影响颜色的地方。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/timePickerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<!-- hour -->
<NumberPicker
android:id="@+id/dialog_hour_np"
android:layout_width="@dimen/picker_width"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_space"
android:focusable="true"
android:focusableInTouchMode="true"
android:solidColor="@color/colorPrimaryDark"
android:theme="@style/AppTheme.Picker"
app:layout_constraintEnd_toStartOf="@+id/dialog_minute_np"
app:layout_constraintStart_toStartOf="parent" />
<!-- minute -->
<NumberPicker
android:id="@+id/dialog_minute_np"
android:layout_width="@dimen/picker_width"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_space"
android:focusable="true"
android:focusableInTouchMode="true"
android:solidColor="@color/colorPrimaryDark"
android:theme="@style/AppTheme.Picker"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/dialog_amPm_tb"
app:layout_constraintStart_toEndOf="@+id/dialog_hour_np"
app:layout_constraintTop_toTopOf="parent" />
<!-- AM / PM -->
<ToggleButton
android:id="@+id/dialog_amPm_tb"
style="?android:attr/textAppearanceLargeInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/half_space"
android:layout_marginStart="@dimen/std_space"
android:background="@color/colorPrimaryDark"
android:textColor="@color/colorLightAccent"
android:textOff="@string/time_string_pm"
android:textOn="@string/time_string_am"
app:layout_constraintBottom_toBottomOf="@+id/dialog_minute_np"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dialog_minute_np"
app:layout_constraintTop_toTopOf="@+id/dialog_minute_np" />
和主题:
<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:textColorPrimary">@color/colorAccent</item>
<item name="android:textColorSecondary">@color/colorLightAccent</item>
</style>