【问题标题】:how to change number picker style in android?如何更改android中的数字选择器样式?
【发布时间】:2013-02-08 12:14:50
【问题描述】:

我想使用 NumberPicker 组件小部件,但在默认的 Holo 主题中,我需要将蓝色替换为橙色,因为这是我样式中的默认颜色。 如何替换蓝色和数字颜色,并保留组件的所有功能? 谢谢

【问题讨论】:

  • 为什么 Google 不允许设置 NumberPicker 样式

标签: android android-widget android-holo-everywhere


【解决方案1】:

很遗憾,您无法设置它的样式。 NumberPicker 的样式和样式属性不存在于公共 API 中,因此您无法设置它们并更改默认外观。您只能在浅色和深色主题之间进行选择。

作为一种解决方案,我建议改用android-numberpicker 库。该库基本上是从 Android 源代码中提取的 NumberPicker 的一个端口。但它比这更好,它还将NumberPicker 反向移植到Android 2.x。该库可以轻松设置样式。

要设置分隔线的样式,请调整NPWidget.Holo.NumberPicker 样式及其selectionDividerselectionDividerHeight 属性。
要设置文本样式,请调整NPWidget.Holo.EditText.NumberPickerInputText 样式。

【讨论】:

  • 但是通过使用你建议组件的滚动功能丢失的库,我真的很想保留这个功能
  • 库版本也可以滚动。没有问题。
  • 无论我选择什么样式,我总是得到黑色文本。
  • @SomeoneSomewhere 老评论,我知道。您需要在基本主题中覆盖 android:textColorPrimary。这可能可能影响其他 TextView 的文本颜色(如果没有明确设置)。但是,这种行为可以包含在Activity 级别上。
  • 如果它也不能为自己设置文本样式,那么这个库有什么用?我不能覆盖android:textColorPrimary,因为我已经做了不同的事情,
【解决方案2】:

自定义号码选择器预览:

<NumberPicker
  android:id="@+id/np"
  android:layout_width="40dp"
  android:layout_height="40dp"
  android:layout_centerHorizontal="true"
  android:background="@drawable/drawablenp"
  android:layout_centerVertical="true"/>

在名为“drawablenp.xml”的drawable文件夹中创建背景xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:startColor="#707070"
        android:centerColor="#f8f8f8"
        android:endColor="#707070"
        android:angle="270"/>

</shape>

【讨论】:

    【解决方案3】:

    复制 library/res/drawable-*/numberpicker_selection_divider.9.png 并命名,例如 custom_np_sd.9.png。

    通过活动主题覆盖默认的 NumberPicker 样式:

    <style name="AppTheme" parent="@style/Holo.Theme">
      <item name="numberPickerStyle">@style/CustomNPStyle</item>
    </style>
    <style name="CustomNPStyle" parent="@style/Holo.NumberPicker">
      <item name="selectionDivider">@drawable/custom_np_sd</item>
    </style>
    

    并将@style/AppTheme 应用为活动主题。

    【讨论】:

    • 在哪里可以找到库文件夹以复制样式
    • 不工作错误:检索项目的父项时出错:找不到与给定名称“@android:style/Widget.Holo.NumberPicker”匹配的资源。错误:找不到与给定名称匹配的资源:attr 'android:selectionDivider'。
    • 我同意上面的评论。没有找到资源。怎么用?
    【解决方案4】:

    我也遇到过这个问题。我真的很想拥有漂亮的NumberPicker UI。这个问题的所有答案都有效,但非常有限。我几乎创建了自己的RecylerView 来创建我想要的NumberPicker。显然我找到了非常强大的整洁库。这是链接https://github.com/Carbs0126/NumberPickerView

    这里不想回答这个问题。只是想帮助和我有同样问题的人。

    【讨论】:

      【解决方案5】:

      我也遇到这个问题,我用反射来改变样式

      public class MyNumberPicker extends NumberPicker {
          public MyNumberPicker(Context context) {
              super(context);
      
              setNumberPickerDivider();
          }
      
          public MyNumberPicker(Context context, AttributeSet attrs) {
              super(context, attrs);
      
              setNumberPickerDivider();
          }
      
          public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
      
              setNumberPickerDivider();
          }
      
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
              super(context, attrs, defStyleAttr, defStyleRes);
      
              setNumberPickerDivider();
          }
      
          @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);
          }
      
          public void updateView(View view) {
              if (view instanceof EditText) {
                  EditText et = (EditText) view;
                  et.setTextColor(ContextCompat.getColor(getContext(), R.color.font_content));
                  et.setTextSize(16);
              }
          }
      
          private void setNumberPickerDivider() {
      
              try {
                  {
                      Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
                      field.setAccessible(true);
                      field.set(this, ContextCompat.getDrawable(getContext(), R.drawable.horizontal_divider));
                  }
      
                  {
                      Field field = NumberPicker.class.getDeclaredField("mSelectionDividerHeight");
                      field.setAccessible(true);
                      field.set(this, 1);
                  }
              } catch (NoSuchFieldException e) {
                  e.printStackTrace();
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              }
          }
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多