专栏名称: 安卓开发精选
伯乐在线旗下账号,分享安卓应用相关内容,包括:安卓应用开发、设计和动态等。
目录
相关文章推荐
复利大王  ·  娇妻版毛晓彤 ·  14 小时前  
复利大王  ·  年轻时的胡锦涛 ·  14 小时前  
复利大王  ·  江浙沪美女留子回国下嫁怀孕后悔 ·  昨天  
复利大王  ·  湘ya一骨科的瓜? ·  2 天前  
复利大王  ·  老同学中捞一捞能不能找到免费P友 ·  2 天前  
51好读  ›  专栏  ›  安卓开发精选

Android 着色器 Tint 研究

安卓开发精选  · 公众号  · android  · 2017-01-04 21:06

正文

请到「今天看啥」查看全文


originBitmapDrawable , ColorStateList . valueOf ( Color . GREEN )));



背景色是修改成功了 但是这个光标的颜色 还没变 非常不协调, 有人又要说了 我们可以用:



这个xml 属性来修改呀,当然了这个方法确实是可以的 但是你想 你这么做的话 又要增加资源文件了,不是与我们的tint 背道而驰了么?


所以 这个地方 我们就要想办法 突破一下。其实很多人都能想到方法了,对于android 没有 提供给我们的api 比如那些private 函数,


我们通常都是通过反射的方法 去调用的。 这里也是一样,稍微想一下 我们就能明白, 这个地方 我们就先通过反射来获取到这个cursorDrawable


然后给他着色,然后在反射调用方法 给他set进去不就行了么?


首先我们都知道 editext 实际上就是textview,所以我们看一下textview 的源码 找找看 这个属性到底叫啥名字。经过一番努力发现 在这:


// Although these fields are specific to editable text, they are not added to Editor because

// they are defined by the TextView's style and are theme-dependent.

int mCursorDrawableRes ;


并且我们要看下editor的源码 这是和edittext息息相关的:



/**

* EditText specific data, created on demand when one of the Editor fields is used.

* See { @link #createEditorIfNeeded()}.

*/

private Editor mEditor ;



//注意这段代码属于editor

final Drawable [] mCursorDrawable = new Drawable [ 2 ];


有了这段代码 我们就知道 剩下反射的代码怎么写了。


//参数就是要反射修改光标的edittext对象

private void invokeEditTextCallCursorDrawable ( EditText et ) {

try {

Field fCursorDrawableRes = TextView . class . getDeclaredField ( "mCursorDrawableRes" );

// 看源码知道 这个变量不是public的 所以要设置下这个可访问属性

fCursorDrawableRes . setAccessible ( true );

//取得 editext对象里的mCursorDrawableRes 属性的值 看源码知道这是一个int值

int mCursorDrawableRes = fCursorDrawableRes . getInt ( et );

//下面的代码 是通过获取mEditor对象 然后再通过拿到的mEditor对象来获取最终我们的mCursorDrawable这个光标的drawable

Field fEditor = TextView . class . getDeclaredField ( "mEditor" );

fEditor . setAccessible ( true );

Object editor = fEditor . get (







请到「今天看啥」查看全文