Getting custom properties is usually obtained inside a custom view, but in some way it can be obtained in an external style, regardless of the properties of the custom view or the styles in the theme.
Because the external acquisition method of non-custom view is more complex, here is a temporary skip, follow-up.
Custom attr
<?xml version= "1.0" encoding= "Utf-8"? ><resources xmlns:android= "http://schemas.android.com/apk/res/ Android "> <attr name=" theme_name "format=" String|reference "/></resources>
Set attr
<resources> <!--Activity Themes--<style name= "Theme.base" parent= "Android:Theme.Holo.Light" > <item name= "Theme_name" > @string/theme_name_1</item> </style></resources>
Get
Typedvalue outvalue = new Typedvalue (); GetContext (). Gettheme (). Resolveattribute (Android. R.attr.selectableitembackground,outvalue, True); Textview.setbackgroundresource (Outvalue.resourceid);
Note that if I set the data directly, the ResourceID is 0
<resources> <!--Activity Themes--<style name= "Theme.base" parent= "Android:Theme.Holo.Light" > <item name= "Theme_name" > My Themes </item> </style></resources>
At this time our data can be read using typevalue.string
Typedvalue outvalue = new Typedvalue (); GetContext (). Gettheme (). Resolveattribute (Android. R.attr.selectableitembackground,outvalue, True); if (outvalue.data==typevalue.type_string) { Textview.setbackgroundresource (outvalue.string);}
Of course, sometimes our property may be a collection, this time can not be obtained with Typedvalue, typevalued can only judge the type, so we choose the following way
Public int gettabcontainerheight () { typedarray a = mcontext.obtainstyledattributes (null, Android.support.v7.appcompat.r.styleable.actionbar, android.support.v7.appcompat.r.attr.actionbarstyle, 0); int height = a.getlayoutdimension ( android.support.v7.appcompat.r.styleable.actionbar_height, 0); resources r = mcontext.getresources (); if ( !hasembeddedtabs ()) height = Math.min (Height, r.getdimensionpixelsize (android.support.v7.appcompat.r.dimen.abc_action_bar_stacked_max_ height)); a.recycle (); return height; &nbSP;}
For the view internal access method is as follows
private static context Themifycontext (context context, attributeset attrs, int defstyleattr) { TypedArray a = Context.obtainstyledattributes (attrs, android.support.v7.appcompat.r.styleable.toolbar, android.support.v7.appcompat.r.attr.actionbarstyle, 0); Int themeid = a.getresourceid (android.support.v7.appcompat.r.styleable.toolbar_theme, 0); if (themeid != 0) context = new contextthemewrapper (CONTEXT,&NBSP;THEMEID); a.recycle (); return context; }
How to get custom property values in Android theme styles