繁体   English   中英

Android中的Textview包装

[英]Textview wrapping in Android

我必须显示从xml feed获取的Android中的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行,这意味着只需添加...内容即可,例如上面的示例。 我怎样才能做到这一点?

尝试:

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

尝试设置android:ems =“ 10”,它将使您的edittext变宽并包装文本

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

从xml:

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

来自代码:

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

您可以尝试在strings.xmlTextView定义文本。然后,如果您从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)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM