简体   繁体   English

如何在datepicker中更改文本大小?

[英]How to change textsize in datepicker?

The default textsize in datepicker is too big for my app. datepicker中的默认文本大小对我的应用来说太大了。 I've seen a way suggested to change it here , but fishing around for the textviews nested inside the datepicker class seems clunky and error prone. 我已经看到了一种建议在这里改变它的方法,但是为了嵌套在datepicker类中的textviews而钓鱼似乎很笨拙且容易出错。

Is there a better/cleaner way to do it? 有更好/更清洁的方法吗?

The easiest way to change the font size of datepicker/timepicker/numberpicker is customizing the theme. 更改datepicker / timepicker / numberpicker字体大小的最简单方法是自定义主题。

In styles file, set the default font size in the following way. 在样式文件中,按以下方式设置默认字体大小。

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

Setting a theme to DatePicker layout and adding android:textSize to it works for me. 将主题设置为DatePicker布局并将android:textSize添加到它适用于我。

In your layout's xml add a DatePicker applying a theme as shown below - 在你的布局的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"/>

and then define the NumberPickerStyle in styles.xml specifying android:textSize like this - 然后在styles.xml定义NumberPickerStyle ,指定android:textSize如下所示 -

<style name="NumberPickerStyle">
        <item name="android:textSize">@dimen/number_picker_text_size</item>
</style>

use below code: 使用下面的代码:

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's approach made some glitches in the UI of picker itself in case there're a number of pickers and I think it's not perfect to apply text size to all the components under the picker. Sean的方法在选择器本身的UI中出现了一些故障,以防有多个选择器,我认为将文本大小应用于选择器下的所有组件并不完美。 Below is what I've used and it works perfect without any UI glitches. 以下是我使用过的,它完美无任何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>

use below code 使用下面的代码

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

Sean's code works. 肖恩的代码有效。 but it is changing font size for all the other components also(textViews, buttons, etc...) So Here is the solution. 但它也改变了所有其他组件的字体大小(textViews,按钮等......)所以这是解决方案。

Create different style for time picker and use it on time picker theme. 为时间选择器创建不同的样式,并在时间选择器主题上使用它。

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

and use it on your timepicker 并在你的时间选择器上使用它

android:theme="@style/my_time_picker_style"

Look at video, how I did https://youtu.be/JMJ2ujhk9c0 看看视频,我是怎么做的https://youtu.be/JMJ2ujhk9c0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM