得到的TextView背景颜色,而无需使用ColorDrawable(API 11) [英] get background color from textview without using ColorDrawable (API 11)

查看:305
本文介绍了得到的TextView背景颜色,而无需使用ColorDrawable(API 11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能只使用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;
}
 

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;
}

这篇关于得到的TextView背景颜色,而无需使用ColorDrawable(API 11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆