【发布时间】:2013-02-08 12:14:50
【问题描述】:
我想使用 NumberPicker 组件小部件,但在默认的 Holo 主题中,我需要将蓝色替换为橙色,因为这是我样式中的默认颜色。 如何替换蓝色和数字颜色,并保留组件的所有功能? 谢谢
【问题讨论】:
-
为什么 Google 不允许设置 NumberPicker 样式
标签: android android-widget android-holo-everywhere
我想使用 NumberPicker 组件小部件,但在默认的 Holo 主题中,我需要将蓝色替换为橙色,因为这是我样式中的默认颜色。 如何替换蓝色和数字颜色,并保留组件的所有功能? 谢谢
【问题讨论】:
标签: android android-widget android-holo-everywhere
很遗憾,您无法设置它的样式。 NumberPicker 的样式和样式属性不存在于公共 API 中,因此您无法设置它们并更改默认外观。您只能在浅色和深色主题之间进行选择。
作为一种解决方案,我建议改用android-numberpicker 库。该库基本上是从 Android 源代码中提取的 NumberPicker 的一个端口。但它比这更好,它还将NumberPicker 反向移植到Android 2.x。该库可以轻松设置样式。
要设置分隔线的样式,请调整NPWidget.Holo.NumberPicker 样式及其selectionDivider 和selectionDividerHeight 属性。
要设置文本样式,请调整NPWidget.Holo.EditText.NumberPickerInputText 样式。
【讨论】:
android:textColorPrimary。这可能可能影响其他 TextView 的文本颜色(如果没有明确设置)。但是,这种行为可以包含在Activity 级别上。
android:textColorPrimary,因为我已经做了不同的事情,
自定义号码选择器预览:
<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
这里不想回答这个问题。只是想帮助和我有同样问题的人。
【讨论】:
我也遇到这个问题,我用反射来改变样式
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();
}
}
}
【讨论】: