Android Phone font adaptive size in different resolution situations

Source: Internet
Author: User

Two workarounds:

One

1, first, according to different resolutions to obtain a different font size.
Create in Res
Values-480x320/strings.xml inside set <dimen name= "Text_size" >30px</dimen>
And
Values-800x400/strings.xml inside set <dimen name= "Text_size" >30px</dimen>

For 480x320 and 800x400 resolution respectively, the font size is 30px and 40px;

This is called in the Java file

int sizeoftext = (int) this.getresources (). Getdimension (R.dimen.text_size);

Two

1. Get the view width in the onsizechanged of the view, usually the default width is 320, so calculate a zoom ratio
Rate = (float) w/320 W is the actual width
2. Then paint.settextsize (int) (8*rate) when setting the font size; 8 is the font size to be set at a resolution width of 320
Actual font size = default font size x rate

Java code
    1. <textview
    2. Android:layout_width="Fill_parent"
    3. android:layout_height="Wrap_content"
    4. android:text="@string/hello"
    5. Android:textsize="@dimen/text_size"
    6. />



Layout Multi-resolution adaptation

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.