Use Dimen to define dimensions (RPM) in Android

Source: Internet
Author: User

(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:

    1. <?xml version= "1.0" encoding= "Utf-8"?>
    2. <resources> <dimen name= "Text_width" >100px</dimen>
    3. <dimen name= "Text_height" >50px</dimen>
    4. <dimen name= "Btn_width" >30mm</dimen>
    5. <dimen name= "Btn_height" >10mm</dimen>
    6. </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:

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical"
  3. Android:layout_width= "Fill_parent"
  4. android:layout_height= "Fill_parent" >
  5. <textview
  6. android:text= "@string/test_dimen"
  7. Android:id= "@+id/mydimentextview01"
  8. Android:layout_width= "Wrap_content"
  9. android:layout_height= "Wrap_content"
  10. Android:width= "@dimen/text_width"
  11. android:height= "@dimen/text_height"
  12. android:background= "@color/red_bg"/>
  13. <button
  14. android:text= "@string/test_dimen1"
  15. Android:id= "@+id/button01"
  16. Android:layout_width= "Wrap_content"
  17. android:layout_height= "Wrap_content" >
  18. </Button>
  19. </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:

  1. Package yy.android.dimen;
  2. Import android.app.Activity;
  3. Import android.content.res.Resources;
  4. Import Android.os.Bundle;
  5. Import Android.widget.Button;
  6. Import COM.AMAKER.TEST.R;
  7. public class Testdimensionactivity extends Activity {
  8. Private Button MyButton;
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. Super.oncreate (savedinstancestate);
  12. Set the content layout view for the current activity
  13. Setcontentview (R.layout.test_dimen);
  14. Obtaining a button instance through the Findviewbyid method
  15. MyButton = (Button) Findviewbyid (R.ID.BUTTON01);
  16. Obtaining Resources instances
  17. Resources r = getresources ();
  18. Get dimension value by Getdimension method
  19. float Btn_h = r.getdimension (r.dimen.btn_height);
  20. float Btn_w = r.getdimension (r.dimen.btn_width);
  21. Set the width of the button
  22. Mybutton.setheight ((int) btn_h);
  23. Sets the height of the button
  24. Mybutton.setwidth ((int) btn_w);
  25. }
  26. }

Use Dimen to define dimensions (RPM) in Android

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.