2017-09-05 158 views
0

我有maxLines:5ellipsize:end施加TextView,我還使用setMovementMethod(LinkMovementMethod.getInstance())TextView生成鏈接點擊(HTML內容)。的TextView MAXLINES,運動方法和ellipsize

上述所有組合都會禁用正被截斷的文本和要附加的'...'後綴。

任何想法出了什麼問題,以及如何解決它?

沒有設置移動方法,一切都按預期工作。

+0

你能檢查我的答案嗎? – KeLiuyue

回答

-1
<TextView 
     android:id="@+id/html" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:maxLines="5" 
     android:ellipsize="end"/> 

     //inside your activity 
     public class A extends AppCompatActivity{ 

       TextView html = (TextView) view.findViewById(R.id.html); 
       html.setText(Html.fromHtml("this is an example <a href=\"www.google.com\">google</a> link to google")); 
       html.setMovementMethod(LinkMovementMethod.getInstance()); 
       . 
       . 
       . 
     } 
+0

如果您未設置maxLines,則代碼 – aviran

0

試試這個。

方式1。使用setMovementMethodHtml.fromHtml

我沒有設置maxLinesellipsize。它工作正常。

在XML代碼

<TextView 
    android:id="@+id/tv_html" 
    android:ellipsize="end" 
    android:maxLines="5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

然後在Java代碼中

TextView tv_html = (TextView) findViewById(R.id.tv_html); 
tv_html.setText(Html.fromHtml("google:" + "<a href='https://www.google.com.hk'>link to it</a> ")); 
tv_html.setMovementMethod(LinkMovementMethod.getInstance());// make it active 

路2。在XML代碼中使用android:autoLink="all"

<TextView 
    android:id="@+id/tv_html" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="all"/> 

然後在您的java代碼中。

TextView tv_html = (TextView) findViewById(R.id.tv_html); 
tv_html.setText("google: https://www.google.com.hk")); 

路3。在代碼中使用SpannableString

TextView tv_html = (TextView) findViewById(R.id.tv_html); 
SpannableString ss = new SpannableString("google: link to google"); 
ss.setSpan(new URLSpan("https://www.google.com.hk"), 8, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

tv_html.setText(ss); 
tv_html .setMovementMethod(LinkMovementMethod.getInstance()); 

編輯

它可以滾動,但不能顯示...

您可以添加

android:scrollbars="vertical" 
+0

中出現同樣的問題,您如何限制文本?我有一段文本,我想最多顯示5行......它是一個豐富的文本(帶有html),所以我需要使用fromHtml使它跨越 – aviran

+0

我檢查了很多信息並嘗試了很多次。 '......'不顯示。@ aviran – KeLiuyue

0

試試這個代碼。

  <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/white" 
      android:textStyle="bold" 
      android:textSize="20dp" 
      android:id="@+id/txtTitle" 
      android:text="" /> 

      String message="<font color='gray'>"+"YOUR CONTENT"+ "<br>" +"<font color='cyan'>"+"<font size='5'>"+" "+"</font>"; 
      txtTitle.setBackgroundColor(Color.TRANSPARENT); 
      txtTitle.setText(message);