700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android布局添加背景颜色 android-获取布局的背景色

android布局添加背景颜色 android-获取布局的背景色

时间:2023-05-31 19:33:56

相关推荐

android布局添加背景颜色 android-获取布局的背景色

ColorDrawable.getColor()仅适用于11级以上的API,因此您可以使用此代码从API 1级开始支持它。在API 11级以下使用反射。

public static int getBackgroundColor(View view) {

Drawable drawable = view.getBackground();

if (drawable instanceof ColorDrawable) {

ColorDrawable colorDrawable = (ColorDrawable) drawable;

if (Build.VERSION.SDK_INT >= 11) {

return colorDrawable.getColor();

}

try {

Field field = colorDrawable.getClass().getDeclaredField("mState");

field.setAccessible(true);

Object object = field.get(colorDrawable);

field = object.getClass().getDeclaredField("mUseColor");

field.setAccessible(true);

return field.getInt(object);

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

return 0;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。