Android - can I increase the textSize for the NumberPicker widget?

后端 未结 7 621
天命终不由人
天命终不由人 2020-12-08 01:10

We got a NumberPicker widget in 3.0, but it seems that the textSize for this widget can\'t be modified. Am I missing something or is this the case? I\'d really like to incre

相关标签:
7条回答
  • 2020-12-08 01:18

    Jumping to API 23, the solution presented by aheuermann seems to have problems. In the source for NumberPicker, the text size is set based on the EditText child at initialization. If you try to change it later, the EditText field will get big, but all other numbers drawn directly by the widget will still be small.

    The solution I'm going with has the disadvantage that it must be instantiated programmatically, instead of via layout, but otherwise, it seems to work as intended. First, add the following to your styles.xml file:

    <style name="NumberPickerText">
        <item name="android:textSize">40dp</item>
    </style>
    

    Then, you can instantiate the NumberPicker with

        ContextThemeWrapper cw = new ContextThemeWrapper(this, R.style.NumberPickerText);
        NumberPicker np = new NumberPicker(cw);
    

    assuming that it's being done within an Activity. If not, replace the "this" with whatever Context you have available. This code does the equivalent of setting a theme that overrides all TextView sizes within your Activity, but only applies it to the NumberPicker and its children.

    0 讨论(0)
  • 2020-12-08 01:22

    We ran into the same problem and ended up recreating NumberPicker locally. There's a more in depth tutorial here.

    0 讨论(0)
  • 2020-12-08 01:38

    I was able to accomplish this by extending the default NumberPicker. Not ideal, but it works.

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

    Then just reference this class in your layout xml.

    <com.yourpackage.NumberPicker
            android:id="@+id/number_picker"
            android:layout_width="43dp"
            android:layout_height="wrap_content" />
    
    0 讨论(0)
  • 2020-12-08 01:38

    only set this sytle for your 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>
    

    <NumberPicker
            android:theme="@style/AppTheme.Picker"
            android:id="@+id/yearNumberPicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    

    and <item name="android:textColorPrimary">@android:color/white</item> change textcolor :

    0 讨论(0)
  • 2020-12-08 01:38

    The following worked for me (in API 15 / 4.0.3)

    note: aheuermann's solution is safe against potential implementation differences in the NumberPicker's layout across different Android versions (that is, the TextView may not always be at child position 1).

    TextView tv1 = (TextView)_numberPicker.getChildAt(1);
    tv1.TextSize = 10;
    
    0 讨论(0)
  • I was facing the same problem, I wanted to increase the text size of the NumberPicker but couldn't find a way to do it as part of the Widget. All the above answers are great, but instead I went with the following solution, which met my needs:

    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="1.5"
        android:scaleY="1.5"/>
    

    This essentially magnifying the entire Widget.

    0 讨论(0)
提交回复
热议问题