【问题标题】:Android - can I increase the textSize for the NumberPicker widget?Android - 我可以增加 NumberPicker 小部件的 textSize 吗?
【发布时间】:2011-08-05 15:04:45
【问题描述】:

我们在 3.0 中有一个 NumberPicker 小部件,但似乎无法修改此小部件的 textSize。我错过了什么还是是这种情况?我真的很想增加字体大小,默认值很小。但我看不到它的 textSize 属性。

【问题讨论】:

  • 仅供参考,如果您使用 aheuermann 的 解决方案(就像我所做的那样)并且您还想实现一个数字选择器的侦听器,则 onValueChanged 方法必须如下所示。我想我会分享这个,因为我花了一些时间来弄清楚这个。 public void onValueChange(android.widget.NumberPicker picker, int oldVal, int newVal){ //你在值改变时执行的真棒方法 }

标签: android android-3.0-honeycomb


【解决方案1】:

我可以通过扩展默认的 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"));
     }
   }

 }

然后在你的布局xml中引用这个类。

<com.yourpackage.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="43dp"
        android:layout_height="wrap_content" />

【讨论】:

  • 您是否尝试过为 textsize 或 textcolor 使用变量?请检查stackoverflow.com/questions/31804573/…
  • 不错,点赞。需要注意的是,但是我尝试使用较大的字体大小 ~42 进行试验,当环绕轮设置为 false 时,NumberPicker 开始出现异常。
  • 感激不尽。只是不明白,为什么 Android 开发人员没有提供轻松更改 NumberPicker 样式的可能性,显然,这是一个很常见的用例。
  • 在某些方面,Android UI 的创建确实很慢且受到限制,UI 需要时间来开发,在其他一些平台上很容易创建通用控件,而在 Android 中很难创建。
  • 但这不起作用。在 6.0 上,它会更改选定的文本视图,但相邻的文本保持不变,并且只要您移动选择器,字体就会更改为默认值!
【解决方案2】:

仅为您的 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" />

&lt;item name="android:textColorPrimary"&gt;@android:color/white&lt;/item&gt; 更改文本颜色:

【讨论】:

    【解决方案3】:

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

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

    这实际上放大了整个 Widget。

    【讨论】:

      【解决方案4】:

      以下对我有用(在 API 15 / 4.0.3 中)

      注意:aheuermann 的解决方案可以安全地防止不同 Android 版本之间 NumberPicker 布局的潜在实现差异(也就是说,TextView 可能并不总是位于子位置 1)。

      TextView tv1 = (TextView)_numberPicker.getChildAt(1);
      tv1.TextSize = 10;
      

      【讨论】:

        【解决方案5】:

        跳转到 API 23,aheuermann 提出的解决方案似乎有问题。在 NumberPicker 的源代码中,文本大小是根据初始化时的 EditText 子项设置的。如果您稍后尝试更改它,EditText 字段会变大,但小部件直接绘制的所有其他数字仍然会很小。

        我要使用的解决方案的缺点是它必须以编程方式实例化,而不是通过布局,但除此之外,它似乎可以按预期工作。首先,将以下内容添加到您的 styles.xml 文件中:

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

        然后,您可以实例化 NumberPicker

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

        假设它是在一个 Activity 中完成的。如果没有,请将“this”替换为您可用的任何上下文。这段代码相当于设置一个覆盖 Activity 中所有 TextView 大小的主题,但仅将其应用于 NumberPicker 及其子项。

        【讨论】:

        • 替代方案:在自定义视图的构造函数(扩展 NumberPicker)中实例化 ContextThemeWrapper 并将其传递给 super
        • 很好的解决方案,但是对于 TextSize,应该使用 'sp' 而不是 'dp'。
        • 与 compileSdkVersion 29 和 minSdkVersion 26 配合良好。但作为替代方案,我使用 scale &lt;item name="android:scaleX"&gt;1.5&lt;/item&gt; &lt;item name="android:scaleY"&gt;1.5&lt;/item&gt;
        【解决方案6】:

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

        【讨论】:

        【解决方案7】:

        我们可以在 NumberPicker 中自定义视图。
        它具有三个视图-
        2 个 ImageButtons 和 1 个 EditText。

            if(numberPicker.getChildAt(1) instanceOf EditText)
            {
                EditText edt=(EditText)numberPicker.getChildAt(1);
                //do customizations here
            } 
        

        Android 源码中 NumberPicker 的完整布局

        <?xml version="1.0" encoding="utf-8"?>
        <!--
        **
        ** Copyright 2008, The Android Open Source Project
        **
        ** Licensed under the Apache License, Version 2.0 (the "License");
        ** you may not use this file except in compliance with the License.
        ** You may obtain a copy of the License at
        **
        **     http://www.apache.org/licenses/LICENSE-2.0
        **
        ** Unless required by applicable law or agreed to in writing, software
        ** distributed under the License is distributed on an "AS IS" BASIS,
        ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        ** See the License for the specific language governing permissions and
        ** limitations under the License.
        */
        -->
        
        <merge xmlns:android="http://schemas.android.com/apk/res/android">
        
            <ImageButton android:id="@+id/increment"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:drawable/numberpicker_up_btn"
                android:paddingTop="22dip"
                android:paddingBottom="22dip"
                android:contentDescription="@string/number_picker_increment_button" />
        
            <EditText
                android:id="@+id/numberpicker_input"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="@style/TextAppearance.Large.Inverse.NumberPickerInputText"
                android:gravity="center"
                android:singleLine="true"
                android:background="@drawable/numberpicker_input" />
        
            <ImageButton android:id="@+id/decrement"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:drawable/numberpicker_down_btn"
                android:paddingTop="22dip"
                android:paddingBottom="22dip"
                android:contentDescription="@string/number_picker_decrement_button" />
        
        </merge>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-26
          • 2014-03-05
          • 2021-09-12
          • 2016-04-14
          • 2016-02-16
          • 2012-03-05
          • 1970-01-01
          • 2019-11-20
          相关资源
          最近更新 更多