android - TypedArray 的背景颜色
问题描述
我在 Android 中有一个自定义视图,并希望加载由 attrs 和 TypedArray 在自定义视图上设置的所有属性。例如,我在 xml 布局中定义了一个 backcolor 属性,但无法在构造函数中加载它。
我做了一个尝试,但没有成功。
// To be done, get the background color from the attributes.
TypedArray l_typedArray = this.getContext().obtainStyledAttributes(attrs,?? , 0);
// not working Color backColor = l_typedArray.getColor()
解决方案
您可以在此处读取的属性位于 values\attrs.xml 中的 attrs.xml - 可以如下所示
<resources>
<declare-styleable name="bubbleview">
<!-- Color of the playground -->
<attr name="playgroundcolor" format="reference|color" />
<!-- Color of the canon arrow coming out of the top ball -->
<attr name="canoncolor" format="reference|color" />
<!-- Color of the score text -->
<attr name="scorecolor" format="reference|color"/>
</declare-styleable>
</resources>
enter code here
{
TypedArray l_typedArray =
getContext().obtainStyledAttributes(attrs,R.styleable.bubbleview);
m_scorecolor = l_typedArray.getColor(R.styleable.bubbleview_scorecolor,Color.RED);
m_canoncolor = l_typedArray.getColor(R.styleable.bubbleview_canoncolor,Color.BLUE);
l_typedArray.recycle(); // never forget this one!!!
}
推荐阅读
- kedro - 如何在 Kedro 中重现实验或指定节点执行顺序?
- laravel - 有没有办法在不同的域上拥有圣所
- java - 在 Spring Cloud Gateway 配置中解析请求对象主体
- reactjs - 从 localhost:3000 上的 Keycloak 收到 403 Forbidden?
- javascript - 是否可以使用纯 JavaScript 生成视频,就像 html2canvas 可以生成图像一样?
- python - 访问字典中的列表
- flutter - 一些用户在 Flutter 上遇到 socketException
- angular - 星云步进器 NbStepper selectedIndex 不起作用(可能与 Angular 生命周期相关)
- android - 当应用程序在 android 中被杀死时 Workmanager 关闭
- python - 如何优化算法以更快地找到具有fuzzywuzzy的相似字符串?