【问题标题】:How to reduce the font size in NumberPicker如何减小 NumberPicker 中的字体大小
【发布时间】:2014-10-22 08:47:17
【问题描述】:

所有,我正在自定义一个城市选择器,它在内部使用三个 numberPicker,如下所示:

它是一个中国省市区域选择器。

代码:

String[] areas = get_from_somewhere();
areaPicker.setDisplayedValues(areas);

我想减小字体大小,有什么建议吗?

解决方案Refer to this

【问题讨论】:

标签: android numberpicker


【解决方案1】:

您可以为此在自定义类CustomNumberPicker 中扩展默认NumberPicker

public class CustomNumberPicker extends NumberPicker {

    public NumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        if(child instanceof EditText) {
            ((EditText) child).setTextSize(25);
        }
    }
}

现在用CustomNumberPicker替换你当前在xml中的NumberPicker

<com.pkg.name.CustomNumberPicker
    android:id="@+id/number_picker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

【讨论】:

  • 谢谢你的回答,我在 ((EditText) view).setTextSize(25);但什么也没发生
  • 您是否在 xml 或活动中使用了 CustomNumberPicker 而不是 NumberPicker?
  • 是的,我在构造函数中使用“provincePicker = new SmallSizeNumberPicker(context, null);”,断点从不调用。我在 NumberPicker.java 的源代码中搜索关键字 'addView(' ,但一无所获。
【解决方案2】:

虽然技术上不改变字体大小,但可以通过改变整个 NumberPicker View 的比例来改变文本的大小。

<NumberPicker
    android:id="@+id/number_picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleX=".7"
    android:scaleY=".7"/>

【讨论】:

    【解决方案3】:

    基于 John Starbird 的回答。

    你添加样式。

    <style name="NumberPickerText">
        <item name="android:textSize">11sp</item>
    </style> 
    

    在 XML 中为您的数字选择器添加样式。

    android:theme="@style/NumberPickerText"
    

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 2020-12-05
      • 1970-01-01
      • 2014-11-15
      • 2017-08-01
      • 2011-12-17
      • 1970-01-01
      • 2016-02-16
      • 2016-09-18
      相关资源
      最近更新 更多