Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置android复合自定义视图的样式_Android_Android Custom View - Fatal编程技术网

设置android复合自定义视图的样式

设置android复合自定义视图的样式,android,android-custom-view,Android,Android Custom View,请帮我摆脱疯狂 下面的代码可以正常工作并根据需要设置自定义视图的背景: public class MyLayout extends LinearLayout { public MyLayout(Context context, AttributeSet attrs) { super(context, attrs, 0, R.style.MyStyle); } //Omissions } 但它给出了: 调用需要API级别21(当前最小值为19):新建

请帮我摆脱疯狂

下面的代码可以正常工作并根据需要设置自定义视图的背景:

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当前主题中包含 对为样式提供默认值的样式资源的引用 风格颂词。可以为0以不查找默认值

defStyleRes提供的样式资源的资源标识符 StyledAttributes的默认值,仅当defStyleAttr为 在主题中找不到0或0。可以为0以不查找默认值

以下内容不起任何作用:

public class MyLayout extends LinearLayout {

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs, R.style.MyStyle);
    }

    //Omissions

}

我做错了什么?如何简单地用背景色设置自定义组件的样式?

我读到:我希望这不是前进的方向;我认为这在我的情况下不可行是的,这就是
defstylettr
参数的基本用法。为什么对你来说不可行?您只需在资源中添加一个
,在主题中将其值设置为
MyStyle
,并将
R.attr
作为
super
构造函数调用中的第三个参数传递。现在对其进行测试实现,我们将看到它是如何运行的,太多麻烦了!你知道我的需要api 21的解决方案是否有缺点吗?事实上,min api是21。没有更早地引入它真是一件痛苦的事,特别是考虑到许多开发人员认为
defstylettr
实际上就是这样。我们在这里得到了100多票的答案,显示该参数通过了
R.style
,因此它似乎在相当一段时间内造成了混乱。我同意,虽然,这不是一个很好的方式,在第一位。好吧!谢谢是的,我在这方面发现了一些非常混乱的信息。是的,这在我的情况下确实有效。虽然我觉得这真的很傻。。。但是,嘿,它起作用了^^。。。