【问题标题】:Textview wrapping in AndroidAndroid中的Textview包装
【发布时间】:2013-03-18 13:41:15
【问题描述】:

我必须在 Android 中显示来自xml 提要的Textview

让我们把这是我的Textview

Mallika Sherawat's upcoming movie Dirty Politics

这里我为这些Textview动态创建了布局:

LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 50);
textLayoutParams.gravity = Gravity.CENTER;

TextView tv = new TextView(this);
tv.setSingleLine(false);
tv.setEllipsize(null);
tv.setHorizontallyScrolling(false);
tv.setTextColor(Color.parseColor("#a7a9ac"));
tv.setText(Appscontent.Sub_arraylisttwo.get(j));
tv.setLayoutParams(textLayoutParams);
tv.setBackgroundColor(Color.parseColor("#414042"));

我在这里提到了宽度 50。

所以文本在小内容Textview上显示得很好。有时我有更多内容Textview,这意味着我如何显示Textview,如下所示:

Mallika Sherawat's 
upcoming movie Dirty...

我的意思是文本必须在文本扩展后显示最多 2 行,这意味着只需添加 ... 内容,如上例所示。我该怎么做?

【问题讨论】:

    标签: android textview word-wrap


    【解决方案1】:

    尝试:

    tv.setMaxLines(2);
    tv.setEllipsize(TextUtils.TruncateAt.END);
    

    【讨论】:

      【解决方案2】:

      尝试设置 android:ems="10" 它将使您的编辑文本变宽并包裹文本

      【讨论】:

        【解决方案3】:

        尝试将每个 TextView 的 android:ellipsize 设置为 none。有关更多详细信息,请参阅有关此属性的文档。

        来自 xml:

        <TextView ... android:ellipsize="none" />
        

        来自代码:

        TextView textView = new TextView(context);
         ...
         textView.setEllipsize(null);
        

        【讨论】:

          【解决方案4】:

          您可以尝试在strings.xml 中为TextView 定义文本。然后如果您从html 设置,则它可能会根据屏幕大小换行:

          所以你可以试试这个:

          <string name="nice_html">
          <![CDATA[Don&apos;t worry we will never share this with anyone<br/>
          By Clicking register you are indicating that you have read and agreed to the <br/>
           <u>Terms of services</u>  and <u>Privacy Policy</u>
          ]]></string>
          

          然后,在您的代码中:

          TextView foo = (TextView)findViewById(R.id.foo); foo.setText(Html.fromHtml(getString(R.string.nice_html)));
          

          【讨论】:

            猜你喜欢
            • 2020-10-04
            • 1970-01-01
            • 1970-01-01
            • 2023-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多