关于自定义控件和属性时TypedArray.getDimension应当注意的问题
2017-03-14 17:25
573 查看
二、现象说明
我们看到根据屏幕大小的不同,两行文字中下行文字的大小也随之改变,其中摩托罗拉xt910和中兴 v880下面字体的都比上面字体大,而中兴 n760下面字体与上面字体大小是相同的,而华为c8500下面字体比上面字体小。我们再来看看main.xml代码:
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
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
See Also
Unit conversions are based on the current DisplayMetrics associated with the resources.
单位的转换是基于当前资源显示分比率的。
因此我们要慎重使用getDimension定义的属性,不然我们的应用部署到不同的应用上,原先非常美丽的效果,会变得面目全非。
四、解决方案
使用时,默认将字体大小设定好,而在xml配置文件当中删除关于字体大小的设置。如
删除main.xml
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”
配置,使其变成:
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代码中设置好自己想要的字体大小即可,如下:
2
1
2
顶0
踩
我们看到根据屏幕大小的不同,两行文字中下行文字的大小也随之改变,其中摩托罗拉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
踩
相关文章推荐
- 关于自定义控件和属性时TypedArray.getDimension应当注意的问题
- 关于自定义控件和属性时TypedArray.getDimension应当注意的问题
- 自定义控件属性TypedArray和attrs.xml和AttributeSet
- 使用TypedArray设置控件大小注意问题
- 关于get和set访问器以及属性和字段变量的区别问题
- 详解Android自定义控件属性TypedArray以及attrs
- React 关于array.map应该注意的问题
- 自定义控件属性(attr.xml,TypedArray)
- 关于请求方式为GET的form表单,action属性后不能带参数的问题
- 关于请求方式为GET的form表单,action属性后不能带参数的问题
- 关于get和set访问器以及属性和字段变量的区别问题
- 用TypedArray给自定义控件配置属性
- 关于请求方式为GET的form表单,action属性后不能带参数的问题
- 关于array_unshift函数使用的一些问题及其注意事项
- 关于PHP中操作MySQL数据库的一些要注意的问题
- 关于将数据库中的bit型数据绑定到DataGrid中的CheckBox的Checked属性的问题。
- 关于多普达手机上WIFI和GPRS的设置问题注意几点
- 关于PHP中操作MySQL数据库的一些要注意的问题
- 关于对 实体封装的对象 进行数据库操作时需要注意的问题
- 关于ClassName.class.getResourceAsStream的路径问题(转)