自定义DatePicker修改默认颜色和字体,选中颜色和字体,分割线

在这里插入图片描述
1,设置默认的字体大小和颜色
在你的Androidmanifest.xml 对应的 android:theme="" 中添加:

<item name="android:editTextStyle">@style/Widget.EditText.White</item>

//上面所对应的style如下分别是设置默认字体和颜色
//其他方式如直接在android:theme对应的style设置editTextColor,textColor等都无效

 <style name="Widget.EditText.White" parent="@android:style/Widget.EditText">
        <item name="android:textColor">@color/whiter</item>
        <item name="android:textSize">14sp</item>
    </style>

2 自定义 DatePicker 设置分割线以及选中的字体和颜色



import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

import org.elastos.wallet.R;
import org.elastos.wallet.ela.utils.ScreenUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* DatePicker 是由多个NumberPicker组成
* 所有我们需要的是想方设法修改NumberPicker的ui
**/
public class TextConfigDataPicker extends DatePicker {

    public TextConfigDataPicker(Context context) {
        super(context);
    }

    public TextConfigDataPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextConfigDataPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child) {
        this.addView(child, null);


    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        this.addView(child, 0, params);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        updateView(child);
    }

    private void updateView(View view1) {
        if (view1 instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view1;
            //获得所有NumberPicker
            List<NumberPicker> numberPickers = findNumberPicker(viewGroup);
            for (NumberPicker np : numberPickers) {
            		//修改NumberPicker的ui
                for (EditText editText : findEditText1(np)) {
                	//获得所有的edittext并修改ui   这里只有选中的edittext
                    editText.setTextColor(getResources().getColor(R.color.white));
                    editText.setTextSize(16);
                    editText.setGravity(Gravity.CENTER);
                    resizeNumberPicker(np);//修改NumberPicker的宽高
                    np.setWrapSelectorWheel(false);//是否循环
                    np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//是否可编辑

                    np.setOnScrollListener(new NumberPicker.OnScrollListener() {
                        @Override
                        public void onScrollStateChange(NumberPicker view, int scrollState) {
                            view.performClick();//刷新选中状态
                        }
                    });
                }
                try {
                    //设置分割线大小颜色
                    Field mSelectionDivider = getFile("mSelectionDivider");
                    mSelectionDivider.set(np, new ColorDrawable(getResources().getColor(R.color.pickline)));
                    mSelectionDivider.set(np, 1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

  public void updateUI(ViewGroup viewGroup) {
        List<NumberPicker> npList = findNumberPicker(viewGroup);
        for (NumberPicker np : npList) {

            for (EditText editText : findEditText1(np)) {
                editText.setTextColor(getResources().getColor(R.color.white));
                editText.setTextSize(16);
                editText.setGravity(Gravity.CENTER);
                resizeNumberPicker(np);
                np.setWrapSelectorWheel(false);//是否循环
                np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//是否可编辑

                np.setOnScrollListener(new NumberPicker.OnScrollListener() {
                    @Override
                    public void onScrollStateChange(NumberPicker view, int scrollState) {
                        view.performClick();//刷新选中状态
                    }
                });
            }
            try {
                //设置分割线大小颜色
                Field mSelectionDivider = getFile("mSelectionDivider");
                mSelectionDivider.set(np, new ColorDrawable(getResources().getColor(R.color.pickline)));
                mSelectionDivider.set(np, 1);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
    private List<EditText> findEditText1(ViewGroup viewGroup) {
        List<EditText> npList = new ArrayList<EditText>();
        View child = null;
        if (null != viewGroup) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                child = viewGroup.getChildAt(i);
                if (child instanceof EditText) {
                    npList.add((EditText) child);

                } else if (child instanceof LinearLayout) {
                    List<EditText> result = findEditText1((ViewGroup) child);
                    if (result.size() > 0) {
                        return result;
                    }
                }
            }
        }
        return npList;
    }

    private EditText findEditText(NumberPicker np) {
        if (null != np) {
            for (int i = 0; i < np.getChildCount(); i++) {
                View child = np.getChildAt(i);

                if (child instanceof EditText) {
                    return (EditText) child;
                }
            }
        }

        return null;
    }
    /**
     * 得到viewGroup 里面的numberpicker组件
     */
    private List<NumberPicker> findNumberPicker(ViewGroup viewGroup) {
        List<NumberPicker> npList = new ArrayList<NumberPicker>();
        View child = null;
        if (null != viewGroup) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                child = viewGroup.getChildAt(i);
                if (child instanceof NumberPicker) {
                    npList.add((NumberPicker) child);

                } else if (child instanceof LinearLayout) {
                    List<NumberPicker> result = findNumberPicker((ViewGroup) child);
                    if (result.size() > 0) {
                        return result;
                    }
                }
            }
        }
        return npList;
    }


    /**
     * 调整numberpicker大小
     */
    private void resizeNumberPicker(NumberPicker np) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ScreenUtil.dp2px(np.getContext(), 100), ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(ScreenUtil.dp2px(np.getContext(), 5), 0, ScreenUtil.dp2px(np.getContext(), 5), 0);
        np.setLayoutParams(params);
    }

    //反射获取控件 mSelectionDivider mInputText当前选择的view
    public Field getFile(String fieldName) {
        try {
            //设置分割线的颜色值
            Field pickerFields = NumberPicker.class.getDeclaredField(fieldName);
            pickerFields.setAccessible(true);
            return pickerFields;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

用法

 <org.elastos.wallet.ela.utils.widget.TextConfigDataPicker
        style="@style/MyDatePicker"
        android:id="@+id/np"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv_sure"
        android:layout_alignParentBottom="true" />
  		TextConfigDataPicker datePicker = dialog.findViewById(R.id.np);
       // datePicker.updateUI(datePicker);//如果你不想addview是调用updateView 
要在Element UI的el-date-picker组件中修改下拉框选择日期的字体颜色,你可以通过自定义主题或者直接操作CSS样式来实现。以下是两个方法: 1. **使用自定义主题**: - 首先,你需要安装Element UI的主题包,如果还没有安装,可以使用npm或yarn: ```bash npm install element-ui/lib/theme-chalk --save ``` - 创建一个`theme-colors.js`文件,并覆盖`picker-cell-class-name`类的颜色: ```javascript import { createTheme } from 'element-ui'; const customTheme = createTheme({ // 其他配置... components: { DatePicker: { pickerCellClassName: 'your-custom-class', pickerOptions: { cellClassName: (date) => `your-custom-class ${date.disabled ? 'disabled' : ''}` } } }, variables: { datePickerColor: '#你的颜色值' // 或者使用你喜欢的hex、rgb等格式的颜色 }, colors: { Picker: { primary: '#your-color-value' } } }); Vue.use(ElementUI, { theme: customTheme }); ``` 然后在CSS中针对`.your-custom-class`选择器设置颜色: ```css .your-custom-class { color: var(--el-picker-color); } .your-custom-class.disabled { color: rgba(0, 0, 0, 0.25); // 可选,调整禁用状态下的颜色 } ``` 2. **直接操作DOM或使用Vue的 scoped CSS**: 如果你不想更改全局主题,可以在组件内部使用`v-bind:class`动态添加类名并设置颜色: ```html <el-date-picker v-bind:class="{ yourCustomClass: true }" ...></el-date-picker> ``` 然后在组件的样式表中定义 `.yourCustomClass` 类: ```css .yourCustomClass .el-picker-panel__editor-input { color: #你的颜色值; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值