Android - 我可以为NumberPicker小部件增加textSize吗?

Zso*_*agy 28 android android-3.0-honeycomb

我们在3.0中获得了一个NumberPicker小部件,但似乎无法修改此小部件的textSize.我错过了什么或是这样吗?我真的想增加字体大小,它的默认值非常小.但是我看不到它的textSize属性.

ahe*_*ann 90

我能够通过扩展默认的NumberPicker来实现这一目标.不理想,但它的工作原理.

public class NumberPicker extends android.widget.NumberPicker {

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

   @Override
   public void addView(View child) {
     super.addView(child);
     updateView(child);
   }

   @Override
   public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
     super.addView(child, index, params);
     updateView(child);
   }

   @Override
   public void addView(View child, android.view.ViewGroup.LayoutParams params) {
     super.addView(child, params);
     updateView(child);
   }

   private void updateView(View view) {
     if(view instanceof EditText){
       ((EditText) view).setTextSize(25);
       ((EditText) view).setTextColor(Color.parseColor("#333333"));
     }
   }

 }
Run Code Online (Sandbox Code Playgroud)

然后在布局xml中引用此类.

<com.yourpackage.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="43dp"
        android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

  • 不能够感谢你.只是不明白,为什么Android开发人员没有提供轻松改变NumberPicker样式的可能性,显然,这是一个非常常见的用例. (7认同)
  • 在某种程度上,Android UI创建非常缓慢且受限制,UI需要时间来开发,常见的控件很容易在其他一些平台中创建,难以在Android中创建. (2认同)

ArM*_*372 7

只为您的NumberPicker设置以下样式:

   <style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textSize">20dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

<NumberPicker
        android:theme="@style/AppTheme.Picker"
        android:id="@+id/yearNumberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

<item name="android:textColorPrimary">@android:color/white</item> 更改textcolor: 在此处输入图片说明


Sha*_*hah 6

我遇到了同样的问题,我想增加NumberPicker的文本大小,但是找不到作为Widget一部分的方法。以上所有答案都是不错的选择,但是我选择了以下解决方案,它满足了我的需求:

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleX="1.5"
    android:scaleY="1.5"/>
Run Code Online (Sandbox Code Playgroud)

这实际上放大了整个Widget。


Gin*_*ray 2

我们遇到了同样的问题,最终在本地重新创建了NumberPicker这里有更深入的教程。