7

datepicker中的默認文本大小對我的應用來說太大了。我已經看到了一種方法建議將其更改爲here,但是在datepicker類中嵌套的textviews看起來很笨重且容易出錯。如何更改datepicker中的文本大小?

有沒有更好的/更清潔的方式來做到這一點?

回答

1

使用下面的代碼

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

使用下面的代碼:

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); 
+1

爲Android 4.1 +,1「getChildAt(0)」我的工作不是「getChildAt(1)」,當您或旋轉上下2-文字的大小變回,這是在所有 – 2016-04-20 11:59:24

+1

沒有幫助我強烈建議不要使用此解決方案。 'getChildAt(1)',因爲當庫的視圖順序發生變化時它可能會變磚。可能在一個版本上工作,並與不同的實現打破 – 2017-02-08 12:17:02

3

更改日期選擇器/ timepicker/numberpicker的字體大小,最簡單的方法是定製的主題。

在樣式文件中,按照以下方式設置默認字體大小。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="android:textSize">22sp</item> 
</style> 
+0

完美的作品!感謝您的快速解決方案。 – 2017-03-02 17:46:56

0

我已經使用以下CustomDateAndTimePicker。這很容易定製。日期,月份,年份,小時和分鐘等不同選項都有自己的數字選擇器。所以您可以自定義文字大小,顏色和其他樣式。你可以添加自己的號碼選擇器供你自定義使用

0

肖恩的代碼有效。但它也會改變所有其他組件的字體大小(textViews,按鈕等等)。所以這裏是解決方案。

創建時間選擇器的不同風格,並按時間選擇器主題使用它。

<style name="my_time_picker_style"> 
    <item name="android:textSize">24sp</item> 
</style> 

,並用它在你的timepicker

android:theme="@style/my_time_picker_style" 

看視頻,我怎麼沒https://youtu.be/JMJ2ujhk9c0

1

肖恩的做法MADE IN選擇器本身的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> 
相關問題