Skip to content Skip to sidebar Skip to footer

How To Add Cardview Attributes To App Theme?

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 style name='AppTheme' parent='

Solution 1:

publicView(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

<attrname="myCardViewStyle"/>

styles.xml

<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"><itemname="myCardViewStyle">@style/CustomCardView</item></style><stylename="CustomCardView"parent="CardView"><itemname="cardBackgroundColor">@android:color/holo_red_dark</item></style>

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.

Post a Comment for "How To Add Cardview Attributes To App Theme?"