Note when setting the font size dynamically.

Source: Internet
Author: User

Note when setting the font size dynamically.

When we dynamically set the font size for android, we stepped on some pitfalls and listed them to remind everyone.

1. setTextSize () directly. The default unit is sp.

2,

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopViewStyle);

textSize = typedArray.getDimension(R.styleable.TopViewStyle_textSize, TypedValue.applyDimension(TypedValue .COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.title_size), context.getResources().getDisplayMetrics()));

 

Custom Attributes are used to obtain the font size of a custom view, as shown in the code above. However, the Unit obtained in this way is px, so setTextSize () cannot be directly given. Otherwise, the displayed size will change greatly.

 

3. How can we solve this problem?

setTextSize(TypedValue.COMPLEX_UNIT_PX,18); //pxsetTextSize(TypedValue.COMPLEX_UNIT_SP,18); //spsetTextSize(TypedValue.COMPLEX_UNIT_DIP,18);//dip

Use the preceding method to specify the unit. In fact, set setTextSize () directly. The parameter is the default TypedValue. COMPLEX_UNIT_SP: The final font size of the above three lines of code is the same, but the unit is different, all of which are converted by 18sp.

 

Respect the fruits of labor, Mount please indicate the source http://www.cnblogs.com/tangZH/p/8298499.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.