Use dimen to define dimensions in Android

Source: Internet
Author: User

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:

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

  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>

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:

  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 of the current activity
     
  13. Setcontentview (R. layout. test_dimen );
     
  14. // Obtain the button instance using the findviewbyid Method
     
  15. Mybutton = (button) findviewbyid (R. Id. button01 );
     
  16. // Obtain the resources instance
     
  17. Resources r = getresources ();
     
  18. // Obtain the dimension value through the 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 button width
     
  22. Mybutton. setheight (INT) btn_h );
     
  23. // Set the button height
     
  24. Mybutton. setwidth (INT) btn_w );
     
  25. }
     
  26. }


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.