展会信息港展会大全

Android使用尺寸资源 dimens.xml 定义尺寸
来源:互联网   发布日期:2016-01-14 11:13:34   浏览:2426次  

导读:1 在values文件夹下建立名为dimens xml的文件,如下:[html] 1 ?xml version=1 0 encoding=utf-8? 2 resources 3 4 string name=test_dimen文本区域 string 5 ...

1.在values文件夹下建立名为dimens.xml的文件,如下:

[html]

1. <?xml version="1.0" encoding="utf-8"?>

2. <resources>

3.

4.<string name="test_dimen">文本区域</string>

5.<string name="test_dimen1">按钮</string>

6.<dimen name="text_width">150px</dimen>

7.<dimen name="text_height">100px</dimen>

8.<dimen name="btn_width">30mm</dimen>

9.<dimen name="btn_height">10mm</dimen>

10.<color name="red_bg">#f00</color>

11. </resources>

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="test_dimen">文本区域</string>

<string name="test_dimen1">按钮</string>

<dimen name="text_width">150px</dimen>

<dimen name="text_height">100px</dimen>

<dimen name="btn_width">30mm</dimen>

<dimen name="btn_height">10mm</dimen>

<color name="red_bg">#f00</color>

</resources>

2.在layout文件夹下建立名为 test_dimens.xml的文件,如下:

[html]

1. <?xml version="1.0" encoding="utf-8"?>

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3.android:layout_width="fill_parent"

4.android:layout_height="fill_parent"

5.android:orientation="vertical" >

6.

7.<TextView

8.android:text="@string/test_dimen"

9.android:id="@+id/myDimenTextView01"

10.android:layout_width="wrap_content"

11.android:layout_height="wrap_content"

12.android:width="@dimen/text_width"

13.android:height="@dimen/text_height"

14.android:background="@color/red_bg"

15.

16./>

17.<Button

18.android:text="@string/test_dimen1"

19.android:id="@+id/Button01"

20.android:layout_width="wrap_content"

21.android:layout_height="wrap_content"

22./>

23. </LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

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

/>

</LinearLayout>

3.建立类:

[java]

1. package com.dim;

2.

3. import android.app.Activity;

4. import android.os.Bundle;

5. import android.widget.Button;

6. import android.content.res.*;

7. import com.dim.R;

8.

9. public class DimensionActivity extends Activity {

10./** Called when the activity is first created. */

11. private Button btn;

12.@Override

13.public void onCreate(Bundle savedInstanceState) {

14.super.onCreate(savedInstanceState);

15.//设置当前Activity的布局

16.

17.setContentView(R.layout.test_dimens);

18.//获取Button实例

19.btn=(Button)findViewById(R.id.Button01);

20.

21.Resources r=getResources();

22.

23.float btn_h =r.getDimension(R.dimen.btn_height);

24.float btn_w =r.getDimension(R.dimen.btn_width);

25.

26.btn.setHeight((int)btn_h);

27.

28.btn.setWidth((int)btn_w);

29.

30.//setContentView(R.layout.main);

31.}

32. }

package com.dim;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.content.res.*;

import com.dim.R;

public class DimensionActivity extends Activity {

/** Called when the activity is first created. */

private Button btn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//设置当前Activity的布局

setContentView(R.layout.test_dimens);

//获取Button实例

btn=(Button)findViewById(R.id.Button01);

Resources r=getResources();

float btn_h =r.getDimension(R.dimen.btn_height);

float btn_w =r.getDimension(R.dimen.btn_width);

btn.setHeight((int)btn_h);

btn.setWidth((int)btn_w);

//setContentView(R.layout.main);

}

}

赞助本站

人工智能实验室

相关热词: 尺寸 dimens Android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港