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

二、现象说明

我们看到根据屏幕大小的不同,两行文字中下行文字的大小也随之改变,其中摩托罗拉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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值