How to get custom property values in Android theme styles

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.