Here is this topic’s background:

I defined a custom View which extends FrameLayout and contains a TextView, calledMyView here. And I defined custom attribute “myviewtextsize” in attrs.xml for MyView so that clients can set different text size in layout xml for the TextView of MyView.

So far, clients code can set text size like this:

1
2
3
4
5
6
<MyView
android:…

my:myviewtextsize=”@dimen/textsize_24″

/>

The problem is: how to read the client’s text size number and set it to the TextVew of MyView?

In MyView.java,

1
2
3
float textSize = typedArray.getDimension(R.MyView_myviewtextsize, -1);  

this.textView.setTextSize(textSize).

Above code goes wrong. The text size is bigger than it’s supposed to.

Why? It is mixed units problem.

The default method setTextSize(float) assumes you’re inputting sp units (scaled pixels), while the typedArray.getDimension() method returns an exact pixel size.

It can be fixed this by using the alternate setTextSize(TypedValue, float), like below:

1
this.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

This will make sure you’re working with the same units.

Units Problem: How to read text size as custom attr from xml and set it to TextView in java code的更多相关文章

  1. 解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误

    编写python爬虫程序可以在电商.旅游等网站上爬取相关评论数据,这些数据可以用于词云制作.感情词分析.提取关键词等,也可以将爬取下来的数据以自己的方式进行展示.评论数据爬取下来后,就要考虑怎样入库, ...

  2. Problem : (1.2.1) Text Reverse

    #include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...

  3. 2. Using 'dp' instead of 'px' to set text size

    android:textSize="45px"  ==> android:textSize="45dp" 因为Android Phone的手机分辨率各不相 ...

  4. html、text、val、attr、prop区别。this.value和$(this).val()区别以及return用法

    html(): html() 方法返回或设置被选元素的内容 (inner HTML). 当使用该方法读取多个值时,它会返回第一个匹配元素的内容. 当使用该方法设置一个值时,它会覆盖所有匹配元素的内容. ...

  5. 《Android群英传》读书笔记 (2) 第三章 控件架构与自定义控件详解 + 第四章 ListView使用技巧 + 第五章 Scroll分析

    第三章 Android控件架构与自定义控件详解 1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWin ...

  6. 第二章 控件架构与自定义控件详解 + ListView使用技巧 + Scroll分析

    1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWindow将DecorView作为整个应用窗口的根V ...

  7. [java]java语言初探 servlet+jsp架构

    <<head first java>> https://www.tutorialspoint.com/jsp/jsp_architecture.htm JSP Processi ...

  8. Android 拨号器的实现 [视频1]

    Android自带了拨号功能和拨号器 这个是在一个视频里看到的    想写下来记录一下 下面放源代码 /hehe/res/layout/activity_main.xml <RelativeLa ...

  9. java实现xml文件读取并保存到对象

    首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...

随机推荐

  1. C# 文件操作大全

    1.创建文件夹//using System.IO;Directory.CreateDirectory(%%1); 2.创建文件//using System.IO;File.Create(%%1); 3 ...

  2. 9-slice-scaling

    9-slice-scaling http://rwillustrator.blogspot.com/2007/04/understanding-9-slice-scaling.html

  3. 提交表单注意事项<script>11111</script>

    <input name="name" value="" /> 如果在上面表单中 ,填写 <script>alert('111')< ...

  4. Robot Framework--07 变量的声明、赋值及其使用

    转自:http://blog.csdn.net/tulituqi/article/details/7984642 一.变量的声明 1.变量标识符 每个变量都可以用  变量标识符{变量名}    来进行 ...

  5. 部署openssh服务器

    1.安装服务器端软件包 先查看是否已经安装openSSH服务器软件包 # rpm -qa|grep openssh openssh-askpass-.3p1-.el6_6..x86_64 openss ...

  6. 如何使Python完美升级到新版本

    这里提供一种解决的方法 (加上一句话,发现一个新问题:这种方法yum update 后,需要将/usr/bin下的python文件删除,然后执行: # ln -s /usr/local/python2 ...

  7. 【转】【Java】利用反射技术,实现对类的私有方法、变量访问

    java关于反射机制的包主要在java.lang.reflect中,structs,hibernate,spring等框架都是基于java的反射机制. 下面是一个关于利用java的反射机制,实现了对私 ...

  8. Redis 安装,主从配置及Sentinel配置自动Failover

    1.安装redis 首页地址:http://redis.io/ 下载地址:http://download.redis.io/ 下载最新的源码包 tar -zxvf redis-stable.tar.g ...

  9. 轻取帝国CMS管理员密码

    “帝国”CMS是一套著名的PHP整站程序,是国内使用人数最多的PHPCMS程序之一.令人无奈的是,“帝国”虽然把势力壮大了,却忽略了自身防护的建设,结果在黑客攻击下,“帝国”沦陷了.“帝国”CMS曝出 ...

  10. jQuery.Callbacks 源码解读二

    一.参数标记 /* * once: 确保回调列表仅只fire一次 * unique: 在执行add操作中,确保回调列表中不存在重复的回调 * stopOnFalse: 当执行回调返回值为false,则 ...