简体   繁体   English

使textview尺寸适合所有屏幕的最佳解决方案是什么?

[英]What is the best solution to make textview dimensions fit to all screens?

I am generating textview in my app dynamically and i want make its width and height compatible to all screens . 我正在我的应用程序中动态生成textview,我想使其宽度和高度与所有屏幕兼容。 I am using the following code to make that but when testing it on Galaxy s3, i found it very small despite the same dimensions is good in tablets ... So what is the best solution to do that ?? 我正在使用以下代码来做到这一点,但是在Galaxy s3上进行测试时,尽管在平板电脑中尺寸相同,我发现它很小,所以什么是最好的解决方案?

Thanks in advance :) 提前致谢 :)

Configuration config = getResources().getConfiguration();

            int width = 0;
            int height = 0;
            int textSize = 0;

            if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
                width = 30;
                height = 30;
                textSize = 24;
                Log.e("small", "small");
            } else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
                width = 55;
                height = 55;
                textSize = 24;
                Log.e("normal", "normal");
            } else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
                width = 45;
                height = 45;
                textSize = 32;
                Log.e("large", "large");
            } else {
                Log.e("xlarge", "xlarge");
                width = 60;
                height = 60;
                textSize = 40;
            }

            // http://www.designbyexperience.com/px-to-dp-converter/

            spaceViews[i].setLayoutParams(new LayoutParams(width, height));

            spaceViews[i].setGravity(Gravity.CENTER);
            spaceViews[i].setTextColor(Color.WHITE);

您可以尝试使用此github链接android库

Use the method for consitency in all devices: 在所有设备中使用该方法进行一致性:

int dpToPx(final int dp)
{
    return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}

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

相关问题 适合所有移动屏幕的视频的最佳尺寸是多少? - What is the best size for video which can fit for all mobile screens? 如何使我的布局适合所有屏幕 - How to make my layout fit in all screens 如何使按钮适合所有屏幕? - how to make the buttons fit to all screens? Android设计适合所有屏幕 - Android designing to fit all screens 对齐文本(即使其适合文本视图并覆盖所有空间) - justifing a text (that is, make it fit in a textview and cover all space) 如何为适用于所有尺寸手机屏幕的Android应用制作横幅 - How to make banner for the Android App which could fit all sizes of mobile screens Android TextView不适用于所有屏幕 - Android textview not fit for all the screen 定位图像以适合所有尺寸的屏幕 - Positioning images to fit all size screens 什么是在 android 中实现交叉淡入淡出 animation 到 TextView 而没有相对布局可见性更改的最佳解决方案 - What is the best solution to implement Cross-Fade animation to a TextView in android without relative layout visibility change 如何使图像适合 android 中的所有不同屏幕? 我需要 440x300dp 的图片,查看我的截图 - how to make images fit in all different screens in android ? I need images with 440x300dp,check out my screenshots
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM