更改Android的numberpicker分颜色 [英] Change android numberpicker divider color

查看:342
本文介绍了更改Android的numberpicker分颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变上述透明numberpicker的颜色分(蓝色)。
我尝试了很多东西像

I want to change the divider color(blue) of numberpicker shown above to transparent. I tried a lot of things like

number_picker.setDividerDrawable(getResources().getDrawable(R.color.transparent));
number_picker.setShowDividers(NumberPicker.SHOW_DIVIDER_NONE);

我也试过设置的android:分压器,XML
但他们没有工作

I also tried settings android:divider in xml But none of them worked

然后我试图使用样式设置它,但是当我把我的风格以下条目,它说需要的最低版本是14和我的应用程序有最低版本11

Then I tried setting it using styles, but when I put the following entry in my styles, it says minimum version required is 14 and my app has minimum version 11

<style name="AppTheme" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:divider">@color/transparent</item>
</style>

任何人都可以建议我怎么能做到这一点?

Can anyone suggest how can I achieve this?

推荐答案

使用反射,我们可以做到这一点。

Using Reflection,we can achieve this

public DatePickerDialog customDatePicker(OnDateSetListener listener, Calendar cal) 
{
    Calendar c;
    if (cal == null) {
        c = Calendar.getInstance();
    } else {
        c = cal;
    }

    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog newFragment = new DatePickerDialog(this, listener, year, month, day);

    // removes the original topbar:
    newFragment.setTitle(""); 

    // Divider changing:
    DatePicker dpView = newFragment.getDatePicker(); 
    LinearLayout llFirst = (LinearLayout) dpView.getChildAt(0);
    LinearLayout llSecond = (LinearLayout) llFirst.getChildAt(0);
    for (int i = 0; i < llSecond.getChildCount(); i++) {
        NumberPicker picker = (NumberPicker) llSecond.getChildAt(i); // Numberpickers in llSecond
        // reflection - picker.setDividerDrawable(divider); << didn't seem to work.
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    pf.set(picker, getResources().getColor(R.color.green ));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    // New top:
    int titleHeight = 90;
    // Container:
    LinearLayout llTitleBar = new LinearLayout(this);
    llTitleBar.setOrientation(LinearLayout.VERTICAL);
    llTitleBar.setBackground(new ColorDrawable(Color.parseColor("#00afac")));
    llTitleBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, titleHeight));

    // TextView Title:
    TextView tvTitle = new TextView(this);
    tvTitle.setText("Select Birthday");
    tvTitle.setGravity(Gravity.CENTER);
    tvTitle.setPadding(10, 10, 10, 10);
    tvTitle.setTextSize(24);
    tvTitle.setTextColor(Color.parseColor("#FFFFFF"));
    tvTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, titleHeight-2));
    llTitleBar.addView(tvTitle);

    // View line:
    View vTitleDivider = new View(this);
    vTitleDivider.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
    vTitleDivider.setBackgroundColor(getResources().getColor(R.color.green));
    llTitleBar.addView(vTitleDivider);

    dpView.addView(llTitleBar);
    FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams) llFirst.getLayoutParams();
    lp.setMargins(0, titleHeight, 0, 0);
    return newFragment;
}

// ----------------

//----------------

调用这个函数

customDatePicker(this,calander).show();

这篇关于更改Android的numberpicker分颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆