[英]How to change textsize in datepicker?
datepicker中的默認文本大小對我的應用來說太大了。 我已經看到了一種建議在這里改變它的方法,但是為了嵌套在datepicker類中的textviews而釣魚似乎很笨拙且容易出錯。
有更好/更清潔的方法嗎?
更改datepicker / timepicker / numberpicker字體大小的最簡單方法是自定義主題。
在樣式文件中,按以下方式設置默認字體大小。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textSize">22sp</item>
</style>
將主題設置為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>
使用下面的代碼:
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);
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>
使用下面的代碼
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);
肖恩的代碼有效。 但它也改變了所有其他組件的字體大小(textViews,按鈕等......)所以這是解決方案。
為時間選擇器創建不同的樣式,並在時間選擇器主題上使用它。
<style name="my_time_picker_style">
<item name="android:textSize">24sp</item>
</style>
並在你的時間選擇器上使用它
android:theme="@style/my_time_picker_style"
看看視頻,我是怎么做的https://youtu.be/JMJ2ujhk9c0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.