Android中使用dimen定義尺寸

來源:互聯網
上載者:User

       最近,遇到了一種定義尺寸的方法,類似於C裡面的宏定義一樣,其實以前已經見過了這種使用方式,今天有時間就記錄一下方便以後使用。下面來介紹一下:

尺寸資源XML檔案的使用
       下面還是通過一個執行個體來示範尺寸資源的用法。該執行個體在布局檔案中添加一個TextView和一個Button,分別使用尺寸資源檔來定義它們的寬和高。
        在工程的res\values\目錄下建立一個dimens.xml尺寸資源檔。

Java代碼:

  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>

       在工程的res\layout\目錄下建立一個test_dimen.xml布局檔案。在該布局檔案中添加一個TextView和一個Button。TextView的寬和高引用尺寸資源來設定。Button的寬和高在代碼中設定。

Java代碼:

  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>

        在yy.android.dimen包中,建立一個TestDimensionActivity類。在該類頂部聲明使用的Button視圖組件,在onCreate()方法中執行個體化該組件,並定義尺寸資源設定其寬和高。

Java代碼:

  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. // 設定當前Activity的內容布局視圖
     
  13. setContentView(R.layout.test_dimen);
     
  14. // 通過findViewById方法獲得Button執行個體
     
  15. myButton = (Button)findViewById(R.id.Button01);
     
  16. // 獲得Resources 執行個體
     
  17. Resources r = getResources();
     
  18. // 通過getDimension方法獲得尺寸值
     
  19. float btn_h = r.getDimension(R.dimen.btn_height);
     
  20. float btn_w = r.getDimension(R.dimen.btn_width);
     
  21. // 設定按鈕的寬
     
  22. myButton.setHeight((int)btn_h);
     
  23. // 設定按鈕的高
     
  24. myButton.setWidth((int)btn_w);
     

  25.  
  26. }


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.