样式化android复合自定义视图

时间:2018-04-26 07:49:28

标签: 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):new   android.widget.LinearLayout

我做了一些手动从样式中提取属性的代码,但它不是动态的 - 所以如果稍后通过添加属性来更改样式,则必须保留一些东西。

我已阅读下面的引文,但不太了解与主题的互动':

  

defStyleAttr当前主题中包含a的属性   引用为其提供默认值的样式资源   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

}

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

0 个答案:

没有答案