Recently, I encountered a method to define the size, similar to the macro definition in C. In fact, I have seen this method before. Today I have time to record it for future use. Here we will introduce:
Use of dimension resource XML files
The following uses an instance to demonstrate the usage of dimension resources. This instance adds a textview and a button to the layout file, and uses the size resource file to define their width and height respectively.
Create a dimens. xml size resource file under the res \ values \ directory of the project.
Java code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Resources> <dimen name = "text_width"> 100px </dimen>
- <Dimen name = "text_height"> 50px </dimen>
- <Dimen name = "btn_width"> 30mm </dimen>
- <Dimen name = "btn_height"> 10mm </dimen>
- </Resources>
Create a test_dimen.xml layout file under the res \ Layout \ directory of the project. Add a textview and a button to the layout file. Textview width and height reference size resource to set. Set the width and height of the button in the code.
Java code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <Textview
- Android: text = "@ string/test_dimen"
- Android: Id = "@ + ID/mydimentextview01"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: width = "@ dimen/text_width"
- Android: Height = "@ dimen/text_height"
- Android: Background = "@ color/red_bg"/>
- <Button
- Android: text = "@ string/test_dimen1"
- Android: Id = "@ + ID/button01"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- </Button>
- </Linearlayout>
Create a testdimensionactivity class in the yy. Android. dimen package. The button view component declared at the top of the class, instantiate the component in the oncreate () method, and define the size resource to set its width and height.
Java code:
- Package yy. Android. dimen;
- Import Android. App. activity;
- Import Android. content. res. Resources;
- Import Android. OS. Bundle;
- Import Android. widget. Button;
- Import com. Amaker. Test. R;
- Public class testdimensionactivity extends activity {
- Private button mybutton;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- // Set the content Layout View of the current activity
- Setcontentview (R. layout. test_dimen );
- // Obtain the button instance using the findviewbyid Method
- Mybutton = (button) findviewbyid (R. Id. button01 );
- // Obtain the resources instance
- Resources r = getresources ();
- // Obtain the dimension value through the getdimension Method
- Float btn_h = R. getdimension (R. dimen. btn_height );
- Float btn_w = R. getdimension (R. dimen. btn_width );
- // Set the button width
- Mybutton. setheight (INT) btn_h );
- // Set the button height
- Mybutton. setwidth (INT) btn_w );
- }
- }