得到的TextView背景颜色,而无需使用ColorDrawable(API 11)颜色、背景、TextView、API

2023-09-09 21:09:18 作者:重蹈覆辙

我怎样才能只使用API​​ 9一个TextView的底色颜色?

我basicly想这样做,但只能使用API​​ 9

  INT INTID =(ColorDrawable)textView.getBackground()的getColor()。
 

解决方案

试试这个...

 公共静态INT getBackgroundColor(TextView中的TextView){
    ColorDrawable绘制=(ColorDrawable)textView.getBackground();
    如果(Build.VERSION.SDK_INT> = 11){
        返回drawable.getColor();
    }
    尝试 {
        场场= drawable.getClass()getDeclaredField(mState)。
        field.setAccessible(真正的);
        Object对象= field.get(绘制);
        字段= object.getClass()getDeclaredField(mUseColor);
        field.setAccessible(真正的);
        返回field.getInt(对象);
    }赶上(例外五){
        // TODO:处理异常
    }
    返回0;
}
 

色彩在UI设计中的应用

How can i get the backround color of a textview using only API 9?

I basicly want to do this but only using API 9

int intID = (ColorDrawable) textView.getBackground().getColor();

解决方案

try this...

public static int getBackgroundColor(TextView textView) {
    ColorDrawable drawable = (ColorDrawable) textView.getBackground();
    if (Build.VERSION.SDK_INT >= 11) {
        return drawable.getColor();
    }
    try {
        Field field = drawable.getClass().getDeclaredField("mState");
        field.setAccessible(true);
        Object object = field.get(drawable);
        field = object.getClass().getDeclaredField("mUseColor");
        field.setAccessible(true);
        return field.getInt(object);
    } catch (Exception e) {
        // TODO: handle exception
    }
    return 0;
}

 
精彩推荐
图片推荐