How to add CardView attributes to app theme?

前端 未结 1 1403
深忆病人
深忆病人 2021-01-04 21:04

My question is similar to \"How to put a CardView attribute in a style\", but I need to go deeper.

I\'m using AppCompat theme, and my styles looks like



        
1条回答
  •  逝去的感伤
    2021-01-04 21:19

    public View(Context context, AttributeSet attrs, int defStyleAttr)
    

    @param defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

    So you have to do

    attrs.xml

    
    

    styles.xml

    
    
    
    

    MyCardView.java

    public MyCardView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.myCardViewStyle);
    }
    
    public MyCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    

    That's it.

    0 讨论(0)
提交回复
热议问题