简体   繁体   中英

Android textsizes different screens

I'm building a android application that need to look allright on all screen sizes. I've managed to scale drawables according to resolution with different drawable resource folders, however how can i achieve the same with pure text?

Do i really need to use different layout folders to achieve this?

Thanks for any help!

Use sp for text sizes and you should be fine.

sp

Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

from Dimensions

dp / dip can also work, but doesn't include the users font preferences.

I used an own-written method for that.. worked for two displays(1024x720, 800x480), hope it works for yours, too

The Class:

       //this is a seperated class, for button textsize
       //declaration of standardvalues

       public class Fontstyles {
            static int INT_standardwidth = 1024, INT_standardsize = 50; //do not change

            public static double BTN_TXT_size(Button BTN_X) {
                Point size = new Point();
                YourMainActivity.DIS_main.getSize(size);
                int INT_width = size.x;
                if (BTN_X.getText().length() == 0)
                    return 0;
                // width of display                 (INT_width)
                // width standartvalue              (INT_standardwidth)
                // standardfontsize                 (INT_standardsize)
                return ((float) ((INT_width * 100 / INT_standardwidth)
                        * (INT_standardsize * (INT_width * 100 / INT_standardwidth) / 100) / 100));
            }}

this is in YourMainAvtivity:

public static Display DIS_main;
DIS_main = getWindowManager().getDefaultDisplay();

and this is for calling the sizecalculation (wherever you want from):

YourButton.setTextSize((float) Fontstyles.BTN_TXT_size_(YourButton));

works for TextViews and Button .

after use please reply and tell me wether this worked for your display or not.

If it is for only textview. declare the size ad dp in textview in xml. dp is desipixels which is related to the screen resolution and the textview size gets adjusted accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM