Units Problem: How to read text size as custom attr from xml and set it to TextView in java code

原创 2013年12月05日 11:08:43

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:

<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,

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:

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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android: How to download the latest zip Android Source Code easily and using it in Intellij

http://geekycoder.wordpress.com/2009/07/03/how-to-download-latest-zip-android-source-code-and-using-...

How to read and write JSON files in Java(Gson)

JSON (JavaScript Object Notation) is a data interchange format widely used in many environments. ...

From Java code to Java heap

https://www.ibm.com/developerworks/library/j-codetoheap/

How to read Json file or text file in Spark

Currently spark is most popular so that I want to learn it. Not because deal with data speed more th...
  • zhq88
  • zhq88
  • 2016年10月12日 16:09
  • 336

(极力推荐)BW:How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files

这篇文章,真是救了哥,所以非常改写这位作者。你看,人家也是2011年2月3号才写的,量身打造,多好的人,刚好赶上我了。    PS:前段时间设计的文本抽取方式被否,为了去R3重建数据源,我找到...
  • asdfak
  • asdfak
  • 2011年02月18日 15:50
  • 4935

How to build NCL and NCAR Graphics from source code

NCAR   CISL  VETS  Download  Contributors  Citing NCL   NCL   Examples  Function...
  • xztjhs
  • xztjhs
  • 2013年06月11日 10:35
  • 2305

How To Read and Write XML Documents with GDataXML

转载自:http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml ...

How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单

How to create custom navigation menu in SharePoint with XML data source Did you ever wanted to crea...

how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language

This article describes how to use a SQLite database in a standalone program with an HTML interface a...

How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268

How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
举报原因:
原因补充:

(最多只允许输入30个字)