Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 根据当前设置的主题获取attr颜色值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的活动中,我正在维护一个SuperActivity,我正在设置主题.
public class SuperActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setTheme(R.style.myThemE);
    }
}

的themes.xml

<!-- ImageBACkround -->
<style name="Theme.myTheme" parent="ThemeLight">
    <item name="myBgColor">@color/translucenT_Black</item>
</style>

现在我想在我的一个孩子活动中获取这种颜色.

正如在这个可能的answer中所提到的,我写道:

int[] attrs = new int[] { R.attr.myBgColor /* index 0 */};
TypedArray ta = ChildActivity.this.obtainStyledAttributes(attrs);
int color = ta.getColor(0,android.R.color.BACkground_light);
String c = getString(color);
ta.recycle();

但每次我得到android.R.color.BACkground_light&的值的值.不是R.attr.myBgColor.

哪里我做错了.我是否通过了ChildActivity.this的错误背景?

解决方法

你有两个可能的解决方案(一个是你实际拥有的,但我为了完整性而包括两个):
TypedValue typedValue = new TypedValue();
if (context.getTheme().resolveAttribute(R.attr.xxx,typedValue,truE))
  return typedValue.data;
else
  return Color.TRANSPARENT;

要么

int[] attribute = new int[] { R.attr.xxx };
TypedArray array = context.getTheme().obtainStyledAttributes(attributE);
int color = array.getColor(0,Color.TRANSPARENT);
array.recycle();
return color;

Color.TRANSPARENT可以是任何其他认值.是的,正如您所怀疑的那样,背景非常重要.如果您继续获取认颜色而不是真实颜色,请查看您传递的上下文.我花了几个小时搞清楚,我试图省去一些打字,只是使用了getApplicationContext(),但它找不到那些颜色……

大佬总结

以上是大佬教程为你收集整理的android – 根据当前设置的主题获取attr颜色值全部内容,希望文章能够帮你解决android – 根据当前设置的主题获取attr颜色值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。