繁体   English   中英

如何在android中更改数字选择器样式?

[英]how to change number picker style in android?

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

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

作为解决方案,我建议改用android-numberpicker库。 该库基本上是从 Android 源代码中提取的 NumberPicker 端口。 但它比这更好的,这也反向移植NumberPicker到Android 2.x的图书馆可以很容易地设计。

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

自定义数字选择器的预览:

在此处输入图片说明

<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>

复制 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 作为活动主题。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM