我想使用NumberPicker组件小部件,但是我需要将默认的Holo主题中的蓝色替换为橙色,因为这是我的样式中的默认颜色。我该如何替换蓝色和数字的颜色,并保留组件的所有功能?
谢谢
![Holo中的默认数字选择器](https://istack.dev59.com/cCvX7.webp)
NumberPicker
的样式和样式属性不存在于公共API中,因此您无法设置它们并更改默认外观。 您只能在浅色和深色主题之间进行选择。NumberPicker
回退到Android 2.x。 该库可以轻松地进行样式设置。NPWidget.Holo.NumberPicker
样式及其selectionDivider
和selectionDividerHeight
属性。NPWidget.Holo.EditText.NumberPickerInputText
样式。android:textColorPrimary
。这可能会影响其他 TextView 的文本颜色(如果没有显式设置)。但是,此行为可以在 Activity
级别上进行控制。 - Vikram自定义数字选择器的预览:
<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"/>
在drawable文件夹中创建名为"drawablenp.xml"的背景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应用为活动主题。
我也遇到了这个问题,我使用反射来改变样式。
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();
}
}
}