【问题标题】:Set individual lines of TextView to different width将 TextView 的单行设置为不同的宽度
【发布时间】:2012-08-16 23:21:30
【问题描述】:

我正在尝试设置一个包含两行文本的 TextView,第二行比第一行短。

有没有办法设置 TextView 的 Layout 实例(通过 TextView.getLayout() 获取)来截断第二行,或者还有其他我不知道的方法?

编辑 1

我的意图被误解了:我试图设置一个布局的文本视图,我可以设置任意文本,并且根据长度会发生以下情况: 如果文本足够长以开始第二行,则使第一行与 textview 的内容宽度一样宽,但第二行应该更短,例如宽度的一半。但如果文本足够长,应该有第二行。

我检查了抽象类布局和它的子类,有一个函数

public int getLineEnd(int line)

返回一行最后一个字符的位置。一种方法是设置文本,然后检查第二行的布局宽度,然后将文本更改为较短的版本。无论如何,这似乎是一种 hacky 方式,如果我可以自己设置一个 Layout 实例(通过 setter 方法,例如 getter

TextView.getLayout().

有没有人将自定义布局设置为 Textview 的子类?

EDIT 2 / 我自己的解决方案

找到了解决办法: 现在我正在检查第二行是否与导致第二行变短的区域重叠。如果是这样,我将截断文本。 我使用的函数:

TextView.getLineCount()
TextView.getLayout().getLineStart(int line)
TextView.getLayout().getLineEnd(int line)
TextView.getPaint().measureText()

也许这对未来的任何人都有帮助。

不过,如何设置自己的布局的问题仍未得到解答。

【问题讨论】:

标签: android layout textview


【解决方案1】:

尝试在您的 XML 中使用 android:maxLines="1" 或在您的 Activity 中使用 textView.setMaxLines(1);

【讨论】:

【解决方案2】:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLines

使 TextView 最多有这么多行。

必须是整数值,例如“100”。

这对应于全局属性资源符号maxLines。

你也可以使用:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

如果设置,会使长于视图宽度的单词 椭圆形而不是在中间折断。你也会经常想要 设置 scrollHorizo​​ntally 或 singleLine 以及使文本作为 整体也被限制在一行而不是仍然允许 分成多行。

必须是以下常量值之一。

none start middle end marquee 这对应于 全局属性资源符号椭圆大小。

【讨论】:

    【解决方案3】:

    您可以使用“\n”来标记字符串中的不同行。并使用 TextView.setGravity() 方法来定义 TextView 的 Layout。例如,

    mTextView.setText("Hello World Hello World\nSecond.");
    mTextView.setGravity(Gravity.CENTER);
    

    这个TextView的预览会是:

    Hello World Hello World
            Second
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,这是我的解决方案:


      这里是代码,计算文本长度和截断的truncateText方法。

      public class LastLineControlTextView extends TextView{
      
      private int mMaxLines;
      private float mLastLineRatio = 0.5f;
      private float mLastLineWidth;
      private float mAllowLength;
      
      public LastLineControlTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
      
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          mLastLineWidth = getWidth()*mLastLineRatio;
          mAllowLength = (mMaxLines-1)*getWidth()+mLastLineWidth;
          if(!TextUtils.isEmpty(getText())){
              truncateText(getText());
          }
      }
      
      @Override
      public void setText(CharSequence text, BufferType type) {
          super.setText(text, type);
          if(mAllowLength==0)
              return;
          truncateText(text);
      }
      
      @Override
      public void setMaxLines(int maxlines) {
          super.setMaxLines(maxlines);
          mMaxLines = maxlines;
      }
      
      private void truncateText(CharSequence text){
          float width = Layout.getDesiredWidth(text, getPaint());
          int end = text.length();
          while(width > mAllowLength){
              width = Layout.getDesiredWidth(text, 0, end, getPaint());
              end--;
          }
          if(end==text.length())
              return;
          CharSequence processed = text.subSequence(0, end);
          System.out.println("end:"+end+"processed:"+processed);
          setText(processed);
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 2013-07-13
        相关资源
        最近更新 更多