(Transferred from: http://blog.csdn.net/yuzhiboyi/article/details/7696174)
Recently, encountered a definition of the size of the method, similar to the definition of macro in C, in fact, have seen the use of this way, today there is time to record a convenient later use. Here's a look at:
Use of the dimension resource XML file
The following is a demonstration of the use of dimension resources using an instance. The instance adds a TextView and a button to the layout file, using the dimension resource files to define their width and height, respectively.
Create a Dimens.xml size resource file in the project's Res\values\ directory.
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 in the project's res\layout\ directory. Add a TextView and a button to the layout file. TextView the width and height of the reference size resource to set. The width and height of the button are set 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>
In the Yy.android.dimen package, create a testdimensionactivity class. The button view component that is declared at the top of the class, instantiates the component in the OnCreate () method, and defines the size resource setting with 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 for the current activity
- Setcontentview (R.layout.test_dimen);
- Obtaining a button instance through the Findviewbyid method
- MyButton = (Button) Findviewbyid (R.ID.BUTTON01);
- Obtaining Resources instances
- Resources r = getresources ();
- Get dimension value by Getdimension method
- float Btn_h = r.getdimension (r.dimen.btn_height);
- float Btn_w = r.getdimension (r.dimen.btn_width);
- Set the width of the button
- Mybutton.setheight ((int) btn_h);
- Sets the height of the button
- Mybutton.setwidth ((int) btn_w);
- }
- }
Use Dimen to define dimensions (RPM) in Android