您的位置:首页 > 移动开发 > Android开发

关于自定义控件和属性时TypedArray.getDimension应当注意的问题

2017-03-14 17:25 573 查看
二、现象说明
我们看到根据屏幕大小的不同,两行文字中下行文字的大小也随之改变,其中摩托罗拉xt910和中兴 v880下面字体的都比上面字体大,而中兴 n760下面字体与上面字体大小是相同的,而华为c8500下面字体比上面字体小。我们再来看看main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.yang"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/background">

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@android:color/black"
android:textSize="18sp"
android:text="@string/shiyan"/>
<com.yang.widget.TextViewAndSpaner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
test:item_content_text="@string/shiyan"
test:item_content_textsize="18sp"
test:item_content_textcolor="@android:color/black"
test:item_option_prompt="@string/please_select"
test:item_option_values="@array/tureorfalse" />

</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
TextView设置的字体大小为18sp,而com.yang.widget.TextViewAndSpaner设置的字体大小也为18sp
com.yang.widget.TextViewAndSpaner读取字体大小的代码如下:
float contentSize = typeArray.getDimension( 

R.styleable.TextViewAndSpaner_item_content_textsize, 15); 

Logger.d(“contentSize is ” + contentSize);
四个手机打印出自定义属性来的字体大小为:
摩托罗拉xt910 

09-08 15:07:00.685: D/CustomWidget(4937): contentSize is 27.0
中兴 v880 

09-08 15:15:04.685: D/CustomWidget(4937): contentSize is 27.0
中兴 n760 

09-08 15:05:15.640: D/CustomWidget(31487): contentSize is 18.0
华为c8500 

09-08 15:22:48.565: D/CustomWidget(2018): contentSize is 13.5
三、注意事项
以上我们看到使用自定义属性的
typeArray.getDimension
是不靠谱的 ,字体的大小会根据不同屏幕的分辨率大小而改变字体的大小。我们再来看看关于getDimension
public float getDimension (int id) 

Since: API Level 1
Retrieve a dimensional for a particular resource ID. Unit conversions are based on the current DisplayMetrics associated with the resources. 

Parameters 

id The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. 

Returns
Resource dimension value multiplied by the appropriate metric.

Throws 

Resources.NotFoundException Throws NotFoundException if the given ID does not exist. 

See Also
getDimensionPixelOffset(int)
getDimensionPixelSize(int)

Unit conversions are based on the current DisplayMetrics associated with the resources.
单位的转换是基于当前资源显示分比率的。
因此我们要慎重使用getDimension定义的属性,不然我们的应用部署到不同的应用上,原先非常美丽的效果,会变得面目全非。 

四、解决方案
使用时,默认将字体大小设定好,而在xml配置文件当中删除关于字体大小的设置。如
删除main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.yang"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/background">

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@android:color/black"
android:textSize="18sp"
android:text="@string/shiyan"/>
<com.yang.widget.TextViewAndSpaner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
test:item_content_text="@string/shiyan"
test:item_content_textsize="18sp"
test:item_content_textcolor="@android:color/black"
test:item_option_prompt="@string/please_select"
test:item_option_values="@array/tureorfalse" />

</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
中的
test:item_content_textsize=”18sp”
配置,使其变成:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.yang"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/background">

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@android:color/black"
android:textSize="18sp"
android:text="@string/shiyan"/>
<com.yang.widget.TextViewAndSpaner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
test:item_content_text="@string/shiyan"

test:item_content_textcolor="@android:color/black"
test:item_option_prompt="@string/please_select"
test:item_option_values="@array/tureorfalse" />

</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
同时在读取自定义属性的Java代码中设置好自己想要的字体大小即可,如下:
float contentSize = typeArray.getDimension(
R.styleable.TextViewAndSpaner_item_content_textsize, 15);
1
2
1
2



顶0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android