简体   繁体   English

Android中的Textview包装

[英]Textview wrapping in Android

I have to display the Textview in Android getting from xml feed. 我必须显示从xml feed获取的Android中的Textview

Let us take this is my Textview : 让我们以这是我的Textview

Mallika Sherawat's upcoming movie Dirty Politics

Here I have created the layout dynamically for these Textview : 在这里,我为这些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"));

I have mentioned the width 50 here. 我在这里提到宽度50。

So the text is displayed well on small content Textview .some time I have more content Textview which means how can I display the Textview like below: 因此,文本可以在较小的Textview上很好地显示。有时我会有更多的Textview内容,这意味着如何显示Textview如下所示:

Mallika Sherawat's 
upcoming movie Dirty...

I mean the text is have to display maximum 2 lines only after the text is extended means simply add the ... content like above example. 我的意思是,仅在扩展文本之后,文本才必须显示最多2行,这意味着只需添加...内容即可,例如上面的示例。 How can I do this? 我怎样才能做到这一点?

Try with: 尝试:

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

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

Try setting android:ellipsize to none for each TextView. 尝试将每个TextView的android:ellipsize设置为none。 For more details see documentation on this attribute. 有关更多详细信息,请参见有关此属性的文档。

From xml: 从xml:

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

From code: 来自代码:

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

You can try defining your text for the TextView in strings.xml .Then if you set from html then it may wrap according to screen size: 您可以尝试在strings.xmlTextView定义文本。然后,如果您从html进行设置,则它可能会根据屏幕尺寸进行换行:

So you can try this out: 因此,您可以尝试以下方法:

<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>

Then, in your code: 然后,在您的代码中:

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