在我的应用程序中,日期选择器的默认文本大小太大了。我看到有一种方法建议在这里更改 。但是在日期选择器类中寻找嵌套的textview似乎很笨拙且容易出错。
是否有更好/更清洁的方法来解决?
在我的应用程序中,日期选择器的默认文本大小太大了。我看到有一种方法建议在这里更改 。但是在日期选择器类中寻找嵌套的textview似乎很笨拙且容易出错。
是否有更好/更清洁的方法来解决?
将DatePicker
布局设置为主题并添加android:textSize
对我有用。
在你的布局xml中添加一个DatePicker
,应用如下所示的主题-
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="@style/NumberPickerStyle"
android:datePickerMode="spinner"
android:calendarViewShown="false"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
接着在 styles.xml
中定义 NumberPickerStyle
,并指定像这样的 android:textSize
-
<style name="NumberPickerStyle">
<item name="android:textSize">@dimen/number_picker_text_size</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textSize">22sp</item>
</style>
Sean的方法在存在多个选择器的情况下会导致UI出现一些故障,我认为将文本大小应用于选择器下的所有组件并不完美。以下是我使用的方法,它可以完美地工作而且没有任何UI故障。
<style name="PickerTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:editTextStyle">@style/PickerEditText</item>
</style>
<style name="PickerEditText" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textSize">24sp</item>
</style>
<style name="my_time_picker_style">
<item name="android:textSize">24sp</item>
</style>
并在您的时间选择器上使用它
android:theme="@style/my_time_picker_style"
看这个视频,了解我是如何做到的 https://youtu.be/JMJ2ujhk9c0
ViewGroup childpicker = (ViewGroup)datePicker.findViewById(Resources.getSystem().getIdentifier("month", "id", "android"));
EditText mornthEt = (EditText)childpicker.getChildAt(1);// month widget
//change textsize and textcolor for mornthEt
mornthEt.setTextSize(30);
mornthEt.setTextColor(Color.GREEN);
getChildAt(1)
,因为当库内视图的顺序改变时,它可能会变得无法使用。在一个版本上可能有效,在不同的实现中可能会出现问题。 - carlo.marinangeli DatePicker picker = (DatePicker)findViewById(R.id.dp_date);
ViewGroup childpicker
= (ViewGroup)
findViewById(Resources.getSystem().getIdentifier("month" /*rest is:
day, year*/, "id", "android"));
EditText textview = (EditText)
picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input",
"id", "android"));
textview.setTextSize(30);
textview.setTextColor(Color.GREEN);