Home » Android » Android, how to get the attr property in the value...

Android, how to get the attr property in the value of the code

Gets the value of ARRT

sometimes we need to color, as attr numerical attributes, this is done in order to shield developers corresponding to a specific value, for example, we need to set the main color, different themes under the secondary color, or a different version of the ActionBar size, or under different Dpi DrawerLayout width.


in XML, we can easily reference attr attribute values, such as:


android:background= "attr/colorPrimary?""
Android:minHeight= "attr/actionBarSize"

of course, we sometimes need to get the attr property value in the code:


TypedValue typedValue = new, TypedValue ();
Context.getTheme ().ResolveAttribute (R.attr.yourAttr, typedValue, true);
For / / String
TypedValue.string
TypedValue.coerceToString ()
Other data / / For
TypedValue.resourceId
TypedValue.data;

Gets the value in the ARRT style

above is for individual values, according to different types to obtain, and if you want to get style, you need to get the resourceId after further access to specific values, to TextAppearance.Large as an example:



TypedValue typedValue = new, TypedValue ();
Context.getTheme ().ResolveAttribute (android.R.attr.textAppearanceLarge, typedValue, true);
Int[] attribute = new int[] {android.R.attr.textSize};
TypedArray array = context.obtainStyledAttributes (typedValue.resourceId, attribute);
Int textSize = array.getDimensionPixelSize (0 / * * / index, -1 / * default * / size);
Array.recycle ();

note that remember to call the TypedArray.recycle () method to recycle the resource.


Last

seems to be annoying, but in fact it should be a fool's thinking, which is directly obtained by different methods, such as:


getValueOfColorAttr (int, attr)

GetValueOfTextSizeAttr (int, style, int, value)


transfers from: http://solo.farbox.com/blog/how-to-get-value-of-attr-in-code#toc_0

Latest