【问题标题】:What is the best solution to make textview dimensions fit to all screens?使 textview 尺寸适合所有屏幕的最佳解决方案是什么?
【发布时间】:2014-05-30 20:19:19
【问题描述】:

我在我的应用程序中动态生成 textview,我想让它的宽度和高度与所有屏幕兼容。 我正在使用以下代码来制作它,但是在 Galaxy s3 上对其进行测试时,我发现它非常小,尽管平板电脑的尺寸相同...... 那么最好的解决方案是什么??

提前致谢:)

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);

【问题讨论】:

  • 使用sp作为文本视图大小,如android:textSize="18sp"
  • 我不说文本本身这个textview有背景而且这个背景很小

标签: android textview width


【解决方案1】:

你可以试试这个github链接android library

【讨论】:

    【解决方案2】:

    在所有设备中使用一致性方法:

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

    【讨论】:

      猜你喜欢
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多