如何减少NumberPicker中的字体大小

时间:2014-10-22 08:47:17

标签: android numberpicker

全部,我自定义一个城市选择器,里面使用三个numberPicker,如下所示: enter image description here

它是中国的省 - 市区选择者。

代码:

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

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

解决方案Refer to this

3 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:7)

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

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

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

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

答案 2 :(得分:2)

基于 John Starbird 回答。

添加样式。

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

将样式添加到XML中的数字选择器。

android:theme="@style/NumberPickerText"
相关问题