简体   繁体   English

Android文字大小不同的屏幕

[英]Android textsizes different screens

I'm building a android application that need to look allright on all screen sizes. 我正在构建一个需要在所有屏幕尺寸上正常显示的android应用程序。 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作为文本大小,应该没问题。

sp SP

Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. 与缩放无关的像素-就像dp单位一样,但是它也通过用户的字体大小首选项进行缩放。 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. dp / dip也可以,但不包括用户的字体首选项。

I used an own-written method for that.. worked for two displays(1024x720, 800x480), hope it works for yours, too 我为此使用了自己编写的方法。.适用于两个显示器(1024x720、800x480),希望它也适用于您的显示器

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: 这是在YourMainAvtivity中:

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

and this is for calling the sizecalculation (wherever you want from): 这是用于调用sizecalculation(无论您要从何处调用):

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

works for TextViews and Button . 适用于TextViewsButton

after use please reply and tell me wether this worked for your display or not. 使用后,请回复并告诉我是否适用于您的显示器。

If it is for only textview. 如果仅用于textview。 declare the size ad dp in textview in xml. 在xml的textview中声明ad dp的大小。 dp is desipixels which is related to the screen resolution and the textview size gets adjusted accordingly. dp是与屏幕分辨率有关的desipixels,因此textview大小会相应调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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