代码之家  ›  专栏  ›  技术社区  ›  Elad Benda

当a11y变为大字体时,android视图宽度不会变大

  •  1
  • Elad Benda  · 技术社区  · 7 年前

    我有一些代码可以测量android textView的宽度和要在其中显示的文本。

    final ViewTreeObserver[] viewTreeObserver = {myAccountView.getViewTreeObserver()};
        viewTreeObserver[0].addOnPreDrawListener(
            new OnPreDrawListener() {
              @Override
              public boolean onPreDraw() {
                myAccountView.setText(R.string.og_my_account_desc_long_length);
                int chipWidth = myAccountView.getMeasuredWidth();
                if (chipWidth > 0) {
                  setChipTextWithCorrectLength(chipWidth);
                  viewTreeObserver[0] = myAccountView.getViewTreeObserver();
                  if (viewTreeObserver[0].isAlive()) {
                    viewTreeObserver[0].removeOnPreDrawListener(this);
                  }
                }
                return true;
              }
            });
      }
    
      private void setChipTextWithCorrectLength(int chipWidth) {
        String desc =
            setChipTextWithCorrectLength(
                getContext().getString(R.string.og_my_account_desc_long_length),
                getContext().getString(R.string.og_my_account_desc_meduim_length),
                getContext().getString(R.string.og_my_account_desc_short_length),
                chipWidth);
        myAccountView.setText(desc);
      }
    
      @VisibleForTesting
      String setChipTextWithCorrectLength(
          String longDesc, String mediumDesc, String shortDesc, int clipWidth) {
        if (textWidthPixels(longDesc) > clipWidth) {
          if (textWidthPixels(mediumDesc) > clipWidth) {
            return shortDesc;
          } else {
            return mediumDesc;
          }
        }
        return longDesc;
      }
    
      public float textWidthPixels(String text) {
        TextPaint textPaint = myAccountView.getPaint();
        return textPaint.measureText(text);
      }
    

    我把我的设备改成了最大的字体和最大的显示器。

    enter image description here

    但是我的测量显示文本宽度(503像素)小于文本视图宽度(587像素)。

    怎么可能呢?

    编辑:

    我试过添加填充物,但没有改变。更改为a11y截断了长文本,而不是选择较短的文本。

      public float textWidthPixels(String text) {
        TextPaint textPaint = myAccountView.getPaint();
        View parent = (View) myAccountView.getParent();
        float width = textPaint.measureText(text);
    
        int paddingLeft = parent.getPaddingLeft();
        int paddingRight = parent.getPaddingRight();
        return width - (paddingLeft + paddingRight);
    
      }
    

    我试图计算芯片宽度考虑其填充,但仍然textSize使文本截断,而它在代码中绘制的大小来短于视图大小

    int chipWidth = myAccountView.getMeasuredWidth() - myAccountView.getPaddingLeft() - myAccountView.getPaddingRight();
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Durgadass S    7 年前

    从图像中,我看到myAccountView向上扩展到圆形边缘。它的宽度延伸到圆边。但文本在左半圆结束后和右半圆开始前开始和结束。所以应该考虑myAccountView的填充。在您的编辑中,您考虑了myAccountView的父级的填充,这与此无关。

    测量切屑宽度时,应考虑衬垫。

    int chipWidth = myAccountView.getMeasuredWidth() - myAccountView.getPaddingLeft() - myAccountView.getPaddingRight();
    
        2
  •  1
  •   Elad Benda    7 年前

    int textViewWidth = textView.getMeasuredWidth();
    

    返回文本视图的宽度,包括任何填充。

    对于附文:

    1. TextPaint paint = textView.getPaint(); int textWidth = (int)paint.measureText(textView.getText().toString());

    2. 使用 getTextBounds()

      TextPaint paint = textView.getPaint(); Rect bounds = new Rect(); paint.getTextBounds(textView.getText().toString(),0,textView.getText().toString().length(),bounds); int textWidth = bounds.width();

    通过将填充值添加到textWidth或从textViewWidth中减去来比较这些值是比较的基础。

    For Difference between the above two ways

    你也可以试试 textView.getLayout().getEllipsisStart(0) 如果您的文本视图是单行的,则直接作为比较基准(而不是计算宽度)。它返回截断文本的索引,否则返回0。

    我也建议你去看看 Autosizing TextViews .