how to change number picker style in android?

后端 未结 5 1202
灰色年华
灰色年华 2020-12-01 21:00

I want to use the NumberPicker component-widget but Instead in the default Holo theme I need to replace the blue color with orange since that is the default color in my styl

5条回答
  •  一个人的身影
    2020-12-01 21:35

    I face this problem too, I use reflect to change the style

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

提交回复
热议问题