android - 样式化 android 复合自定义视图
问题描述
请帮助我摆脱精神错乱!
下面的代码可以根据需要设置自定义视图的背景:
public class MyLayout extends LinearLayout {
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0, R.style.MyStyle);
}
//Omissions
}
但它给出了:
调用需要 API 级别 21(当前最低为 19):新的 android.widget.LinearLayout
我做了一些手动从样式中提取属性的代码,但它不是动态的 - 因此,如果稍后通过添加属性来更改样式,则必须进行维护。
我已阅读下面的引用,但不太了解与“主题”的交互:
defStyleAttr 当前主题中的一个属性,包含对样式资源的引用,该资源为 StyledAttributes 提供默认值。可以为 0 以不查找默认值。
defStyleRes 为 StyledAttributes 提供默认值的样式资源的资源标识符,仅当 defStyleAttr 为 0 或在主题中找不到时使用。可以为 0 以不查找默认值。
以下不做任何事情:
public class MyLayout extends LinearLayout {
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs, R.style.MyStyle);
}
//Omissions
}
我做错了什么?如何简单地使用背景颜色设置自定义组件的样式?
解决方案
推荐阅读
- sql - 使用外键在我的数据库中使用另一个数据库的(混乱)数据
- angular - WeatherCast 应用 Angular 材质
- c++ - 何时使用 __bswap_constant_XX 而不是 __bswap_XX?
- android - 安卓传感器检测温度
- php - 迁移时无法在 Symfony 中使用 ocramius/proxy-manager 进行迁移?
- angular - 离子 3 事件未发布。需要发布数据才能在sidemnu中显示数据
- ios - 获取应用排行榜分数的用户好友用户 ID
- reporting-services - 冻结表格报告的列标题
- perl - 如何让 Perl crypt 加密超过 8 个字符?
- django - 如何在基于 UserCreationForm 的类中添加选择字段?