Android开发学习

android 设置EditText的hint字体大小和光标颜

2018-01-23  本文已影响102人  神情自若

由于默认的hint字体比较丑,我们经常需要自己去设置字体大小和光标样式

/**
     * 设置 hint字体大小
     * @param editText 输入控件
     * @param hintText hint的文本内容
     * @param textSize hint的文本的文字大小(以dp为单位设置即可)
     */
    public static void setHintTextSize(EditText editText, String hintText, int textSize) {
        // 新建一个可以添加属性的文本对象
        SpannableString ss = new SpannableString(hintText);
        // 新建一个属性对象,设置文字的大小
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(textSize, true);
        // 附加属性到文本
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置hint
        editText.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
    }

光标的颜色
1.设置为null,让光标和字体的颜色一样

android:textCursorDrawable="@null" 

2.自定义光标颜色,定义一个资源文件,可以设置光标的颜色和粗细。

android:textCursorDrawable="@drawable/color_cursor" 

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >  
     <size android:width="2dp" />  
    <solid android:color="#FF4081"  />  
</shape>

收工!!!!!!

上一篇 下一篇

猜你喜欢

热点阅读