Android Font Size Adaptive

Source: Internet
Author: User
Tags xml example

Ndroid
If you want to adapt to which resolution in your project RES add values-xxxxxxx (such as: values-values-1024x600) folder, in the folder new Dimens.xml file format as follows: <?xml version= "1.0 "Encoding=" Utf-8 "?><resources> <dimen name=" text_size ">40sp</dimen> </resources> Layout file Reference: <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android: text= "ABCDEFG" android:textsize= "@dimen/text_size"/> A brief description of font units:

px (pixels): The actual pixels of the screen, often said resolution 1024*768pixels, is the transverse 1024px, portrait 768px, different devices display the same effect.
inch (inch): The physical size of the screen, equal to 2.54 centimeters per inch.
mm (mm): The physical size of the screen.
PT (point): The physical size of the screen. 1/72 inches.
Dp/dip : pixels independent of density, a unit of abstraction based on screen density. 1DP = 1px on a monitor 160 dots per inch. However, the ratio of DP and PX varies with the screen density, and different devices have different display effects.
SP : A scale-independent pixel that is used primarily for font display of best for TEXTSIZE, as a unit of text-related size.
Xml:android:textsize= "@dimen/some_name"
Java:float dimen = resources.getdimen (r.dimen.some_name)
XML Example:

· <?xml version= "1.0" encoding= "Utf-8"?>

· <resources>

· <dimen name= "One_pixel" >1px</dimen>

· <dimen name= "Double_density" >2dp</dimen>

· <dimen name= "SIXTEEN_SP" >16sp</dimen>

· </resources>

Calculate a certain percentage of font size by screen size:

    1. public static int getfontsize (int screenwidth, int screenheight) {
    2. screenwidth=screenwidth>screenheight?screenwidth:screenheight;
    3. int rate = (int) (5* (float) screenwidth/320);
    4. Return rate<14?14:rate;
    5. }

Android Font Size Adaptive

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.