如何在日期选择器中更改文本大小?

13
6个回答

10

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>

5
改变日期选择器/时间选择器/数字选择器字体大小的最简单方法是自定义主题。
在样式文件中,按以下方式设置默认字体大小。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textSize">22sp</item>
</style> 

运行得非常好!感谢您提供的快速解决方案。 - Johannes Muenichsdorfer
但这会使项目中的所有默认字体变为22sp,我建议这不是一个好的解决方案。 - Prakash Reddy

3

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>

1
Sean的代码有效,但它也改变了所有其他组件(如textView,button等)的字体大小。因此,这里是解决方案。
为时间选择器创建不同的样式,并在时间选择器主题上使用它。
<style name="my_time_picker_style">
    <item name="android:textSize">24sp</item>
</style>

并在您的时间选择器上使用它

android:theme="@style/my_time_picker_style"

看这个视频,了解我是如何做到的 https://youtu.be/JMJ2ujhk9c0


0
请使用以下代码:
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- 当您向上或向下旋转时,文本大小会改变回去,这一点毫无帮助。 - john-salib
1
我强烈建议不要使用这个解决方案。getChildAt(1),因为当库内视图的顺序改变时,它可能会变得无法使用。在一个版本上可能有效,在不同的实现中可能会出现问题。 - carlo.marinangeli

-2
请使用以下代码。
 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接