Android:如何删除数字选择器中的分隔线

时间:2017-06-13 09:36:58

标签: java android android-linearlayout numberpicker

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

picker.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

XML:

android:showDividers="none"

enter image description here

4 个答案:

答案 0 :(得分:9)

为Numberpicker设置主题

/opt/myapp

style.xml

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

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

答案 1 :(得分:3)

这段代码会更好

private void changeDividerColor(NumberPicker picker, int color) {
        try {
            Field mField = NumberPicker.class.getDeclaredField("mSelectionDivider");
            mField.setAccessible(true);
            ColorDrawable colorDrawable = new ColorDrawable(color);
            mField.set(picker, colorDrawable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

答案 2 :(得分:3)

有一个简单的解决方案。调用picker.setSelectionDividerHeight(0)可以解决问题。

答案 3 :(得分:2)

在您的NumberPicker中添加其中一行

XML:android:selectionDividerHeight="0dp"

OR

JAVA:picker.setSelectionDividerHeight(0)