我当前用于自定义UI的方法是使用常规的android DatePicker,然后使用DatePicketDialog.getDatePicker()提取内部组件并对其进行自定义。

现在结果在图像中https://dl.dropboxusercontent.com/u/3286004/Screen%20Shot%202557-08-29%20at%202.52.21%20AM.png

问题是...我想将“完成”按钮上方的黑线自定义为另一种颜色。
您能否建议我如何取出该线路组件,以便进行更改。

预先谢谢你:D

最佳答案

这是绝对可能的,实际上您可以用它做任何您想做的事情。确实,一种选择是使用样式和主题,但是在4.x中不起作用。可以说,正确或简便的方法是使用视图本身,如下所示:

        // we need this listener since only here all views are really drawn and accessible
        yourDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            private boolean areButtonsFixed;
            @Override
            public void onShow(DialogInterface dialog) {
                if (areButtonsFixed)
                    return;

                // both buttons - you could search for only positive button or whatever button your dialog has
                final Button btnPositive = getButton(DatePickerDialog.BUTTON_POSITIVE);
                final Button btnNegative = getButton(DatePickerDialog.BUTTON_NEGATIVE);
                final Button btnNeutral = getButton(DatePickerDialog.BUTTON_NEUTRAL);

                // buttons layout parameters, change it into material style (gravity right)
                final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
                lp.weight = 0; // was 1 to fill 50% horizontally

                // positive button, set your own label
                btnPositive.setText(R.string.dialog_ok_label);
                // set text color and size
btnPositive.setTextColor(ResHelper.getColor(R.color.blue_bright));
                btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResHelper.getDimensPx(R.dimen.text_size_14));

                btnPositive.setLayoutParams(lp);

                // divider above buttons
                ((LinearLayout) btnPositive.getParent().getParent()).setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
                areButtonsFixed = true;
}


此行(最后一行)将完全删除按钮上方的分隔线。如果要自定义它,请执行以下操作:

((LinearLayout) btnPositive.getParent().getParent()).setDividerDrawable(R.drawable.yer_drawable);

09-10 05:49
查看更多