【问题标题】:textView change to shorter text dynamically instead of ellipsizetextView 动态更改为更短的文本而不是 ellipsize
【发布时间】:2019-02-18 08:34:44
【问题描述】:

我有这个代码:

  final ViewTreeObserver[] viewTreeObserver = {myAcco    
    viewTreeObserver[0].addOnPreDrawListener(
        new OnPreDrawListener() {
          @Override
          public boolean onPreDraw() {
            int chipWidth =
                myAccountView.getMeasuredWidth()
                    - myAccountView.getPaddingLeft()
                    - myAccountView.getPaddingRight();
            if (chipWidth > 0) {
              myAccountView.setText(
                  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));
              viewTreeObserver[0] = myAccountView.getViewTreeObserver();
              if (viewTreeObserver[0].isAlive()) {
                viewTreeObserver[0].removeOnPreDrawListener(this);
              }
            }
            return true;
          }
        });
  }

  @VisibleForTesting
  String setChipTextWithCorrectLength(
      String longDesc, String mediumDesc, String shortDesc, int clipWidth) {
    if (!isTextEllipsized(longDesc, clipWidth)) {
      return longDesc;
    }
    if (!isTextEllipsized(mediumDesc, clipWidth)) {
      return mediumDesc;
    }
    return shortDesc;
  }

  private boolean isTextEllipsized(String text, int clipWidth) {
    TextPaint textPaint = myAccountView.getPaint();

    Toast.makeText(this.getContext(), "textPaint.measureText(text) = "+textPaint.measureText(text)+" clipWidth ="+ clipWidth,
        Toast.LENGTH_LONG).show();


    return textPaint.measureText(text) > clipWidth;
  }

当需要 ellipsize 时,代码应动态更改 textView 中的文本。

但是,当我运行应用程序时,有时我会看到视图(剪辑)宽度 == 长文本宽度,并且 UI 中仍然存在 ellipsize。

我看到当 textView 为 not visible 并切换为 visible 时会发生这种情况。

我应该以不同的方式计算 vlip(视图)宽度吗?

在变成visible之前有什么特殊的方法来测量textView?

【问题讨论】:

  • requestLayout 方法应该计算视图的位置和度量。你试过了吗?

标签: android user-interface textview width android-viewtreeobserver


【解决方案1】:

你应该检查

getEllipsisCount()

查看问题的根源。

定义:返回要省略的字符数,如果没有省略,则返回 0。

【讨论】:

  • 但我只能在太晚的阶段使用getEllipsisCount() 不是吗?什么时候可用? onGlobalLayout(绘制后)。没有?
  • 你不能用它来确定为什么有时它不起作用吗?
  • 检查在 getEllipsisCount() 之前调用 textView.onRtlPropertiesChanged(0) 是否有效?
【解决方案2】:

是的,你可以使用 textView.getLayout().getEllipsisStart(0) ,确保你的 textView 是单行的,你可以通过将 android:maxLines="1" 添加到你的 texView 来做到这一点。它返回文本被截断的索引,否则返回 0。

根据您的观察,可能是在再次将可见性更改为可见后视图没有重新计算,请尝试使用textView.setVisibility(View.GONE) 而不是textView.setVisibility(View.INVISIBLE);。这可能会导致再次调用preDraw()

您也可以考虑使用addOnGlobalLayoutListener() 来跟踪您的视图的可见性变化,对于reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 2017-06-22
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多