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方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...
随机推荐
- 拍拍装修<a href="" > 搜狐 “兼容模式”下失效,解决办法
代码 <a href="http://www.baidu.com"> <p style="></p> <p style=&quo ...
- Elmah 日志记录组件
http://www.cnblogs.com/jys509/p/4571298.html 简介 ELMAH(Error Logging Modules and Handlers)错误日志记录模块和处理 ...
- GoLang之方法与接口
GoLang之方法与接口 Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者的函数 ...
- Mongodb 资源
一.资源 1.C# 驱动下载地址 https://github.com/mongodb/mongo-csharp-driver/releases 2. Mongodb 管理工具 mongochef 下 ...
- Ajax异步刷新地址栏
公司项目后台使用现成的UI框架,DevExpress,jqGrid,XXXUI之类的,这些展示数据列表的控件/插件,基本是异步的. 这倒也好,有变化也只是数据那一块变化,不会重新加载整个页面. 但是, ...
- jQuery 遍历 - find() 方法
实例 搜索所有段落中的后代 span 元素,并将其颜色设置为红色: $("p").find("span").css('color','red'); 详情:htt ...
- linux下如何安装charles
charles是很好的抓包工具. 如何安装呢? 可以去官网下载:http://www.charlesproxy.com/download/ 如果是Debian系列的,可以直接用apt-get 安装 h ...
- Emacs配置文件
;;tab and space;;when true,emacs use mixture of tab and space to archieve(setq-default indent-tabs-m ...
- jQuery回调、递延对象总结(上篇)—— jQuery.Callbacks
前言: 作为参数传递给另一个函数执行的函数我们称为回调函数,那么该回调又是否是异步的呢,何谓异步,如:作为事件处理器,或作为参数传递给 (setTimeout,setInterval)这样的异步函数, ...
- 11个很棒的 jQuery 图表库
如果你曾经使用过任何类型的数据,你应该知道阅读一排排数据的痛苦.通过所有这些数据弄清楚他们的意思是非常不容易的.可视化对于解决这个问题起到了重要的作用.可视化降低了数据阅读的难度,帮助决策者获得可操作 ...