Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
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 |
<MyView |
The problem is: how to read the client’s text size number and set it to the TextVew of MyView?
In MyView.java,
1 |
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的更多相关文章
- 解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误
编写python爬虫程序可以在电商.旅游等网站上爬取相关评论数据,这些数据可以用于词云制作.感情词分析.提取关键词等,也可以将爬取下来的数据以自己的方式进行展示.评论数据爬取下来后,就要考虑怎样入库, ...
- Problem : (1.2.1) Text Reverse
#include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...
- 2. Using 'dp' instead of 'px' to set text size
android:textSize="45px" ==> android:textSize="45dp" 因为Android Phone的手机分辨率各不相 ...
- html、text、val、attr、prop区别。this.value和$(this).val()区别以及return用法
html(): html() 方法返回或设置被选元素的内容 (inner HTML). 当使用该方法读取多个值时,它会返回第一个匹配元素的内容. 当使用该方法设置一个值时,它会覆盖所有匹配元素的内容. ...
- 《Android群英传》读书笔记 (2) 第三章 控件架构与自定义控件详解 + 第四章 ListView使用技巧 + 第五章 Scroll分析
第三章 Android控件架构与自定义控件详解 1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWin ...
- 第二章 控件架构与自定义控件详解 + ListView使用技巧 + Scroll分析
1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWindow将DecorView作为整个应用窗口的根V ...
- [java]java语言初探 servlet+jsp架构
<<head first java>> https://www.tutorialspoint.com/jsp/jsp_architecture.htm JSP Processi ...
- Android 拨号器的实现 [视频1]
Android自带了拨号功能和拨号器 这个是在一个视频里看到的 想写下来记录一下 下面放源代码 /hehe/res/layout/activity_main.xml <RelativeLa ...
- java实现xml文件读取并保存到对象
首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...
随机推荐
- Centos 6.0将光盘作为yum源的设置方法
在使用Centos 的时候,用yum来安装软件包是再方便不过了,但是如果在无法连接互联网的情况下,yum就不好用了. 下面介绍一种方式,就是将Centos安装光盘作为yum源,然后使用yum来安装软件 ...
- 微信H5手指滑动屏蔽微信的默认效果
我们的H5页面放在微信上时,如果你向上滑动或者向下滑动屏幕时,会发现一些微信的特征,譬如:网页由www.baidu.com提供. 去掉这个微信的特征,代码如下: var f; n.addEventLi ...
- ajax传输 基础一
一个简单页面的传输 index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- 使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()
1.适用范围:一些很小的提示或警告音频. 2.使用限制: 声音长度不能超过30秒 声音文件必须是PCM或IMA4(IMA/ADPCM)格式.(有时候可播放一些特殊的.mp3) 打包成.caf..aif ...
- Redis学习——ae事件处理源码分析
0. 前言 Redis在封装事件的处理采用了Reactor模式,添加了定时事件的处理.Redis处理事件是单进程单线程的,而经典Reator模式对事件是串行处理的.即如果有一个事件阻塞过久的话会导致整 ...
- redis+Keepalived实现Redis主从复制
redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave: 10.10.10.204Virtural IP Addres ...
- Effective Objective-C 2.0 — 第13条:用“方法调配 技术” 调试 “黑盒方法”
自己理解是调配了方法 在运行期,可以向类中新增或替换选择子所对应的方法实现. 使用另一份实现来替换原有的方法实现,这道工序叫做“方法调配”,开发者常用此技术向原有实现中添加新功能. 一般来说,只有调试 ...
- Yii2 menu navbar nav小部件的使用示例
menu Menu::widget( [ [ 'label' => $menu['name'], 'url' => [$menu['route']], 'items' => [ [ ...
- 【8-23】node.js学习笔记
Node入门 //请求(require)Node.js自带的 http 模块,并且把它赋值给 http 变量 //变成了一个拥有所有 http 模块所提供的公共方法的对象 var http = req ...
- 如何清理WindowsXp的临时文件
软件限制策略中的 "路径规则" 不允许的, 是指在 这个路径中的程序都 不准运行! 这就限制了 : 通常电脑中病毒, 都是通过上网感染病毒的 -> 病毒/恶意软件通过 &qu ...