安卓:如何在数字选取器中删除分隔线

我如何删除数字选择器中的分隔线,我尝试通过xml将ShowDivider设置为无(似乎不存在)和代码注意工作

picker.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

XML:

android:showDividers="none"

enter image description here


答案 1

为数字收集器设置主题

<NumberPicker
   ...
   android:theme="@style/DefaultNumberPickerTheme" />

风格.xml

<style name="DefaultNumberPickerTheme" parent="AppTheme">
        <item name="colorControlNormal">@color/transparent</item>
</style>

private void changeDividerColor(NumberPicker picker, int color) {

    java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
    for (java.lang.reflect.Field pf : pickerFields) {
        if (pf.getName().equals("mSelectionDivider")) {
            pf.setAccessible(true);
            try {
                ColorDrawable colorDrawable = new ColorDrawable(color);
                pf.set(picker, colorDrawable);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}

并将其设置为

changeDividerColor(yournumberpicker, Color.parseColor("#00ffffff"));

答案 2

在 NumberPicker 中添加其中一行

XML :android:selectionDividerHeight="0dp"

爪哇岛:picker.setSelectionDividerHeight(0)


推荐