2017-09-25 71 views
3

使我有這樣HTML字符串不是TextView的

{ 
"user_id": "27", 
"notification": "<a>There is package wiating for you to pick from <font style="color: blue;"> #Surat</font> to <font style = "color: blue;"> #Adajan, Surat, Gujarat, India</font> </a>", 
}, 
{ 
"user_id": "27", 
"notification": "<a>There is package wiating for you to pick from <font style="color: blue;"> #majuraget, surat</font> to <font style = "color: blue;"> #pal, surat</font> </a>", 
}, 
{ 
"user_id": "27", 
"notification": "<a>There is package wiating for you to pick from <font style="color: blue;"> #majuraget, surat</font> to <font style = "color: blue;"> #pal, surat</font> </a>", 
}, 
{ 
"user_id": "27", 
"notification": "<a>There is package wiating for you to pick from <font style="color: blue;"> #majuraget, surat</font> to <font style = "color: blue;"> #pal, surat</font> </a>", 
} 

一個JSON數據I M渲染的TextView此通知字符串,但它不工作。

這就是我試圖

Spanned value = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { 

      value = Html.fromHtml(notifications.getTitle(), Html.FROM_HTML_MODE_COMPACT); 
      holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance()); 
      holder.txtTitle.setText(value); 

     }else { 
      value = Html.fromHtml(notifications.getTitle()); 
      holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance()); 
      holder.txtTitle.setText(value);  
     } 

我也試過這種library

Spanned fromHtml = HtmlCompat.fromHtml(context, notifications.getTitle(), 0); 
     holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance()); 
     holder.txtTitle.setText(fromHtml); 

,但這些都不是工作。

這是我得到的輸出:

enter image description here

,這是我所期望

enter image description here

+0

你有沒有試過這個https://stackoverflow.co m/questions/42389878/android-app-not-rendering-html-tags-very-well – Ankita

回答

1

嘗試對矯正通知字符串是這樣的:

{ 
"user_id": "27", 
"notification": "<a>There is package wiating for you to pick from <font color='blue'> #majuraget, surat</font> to <font color='blue'> #pal, surat</font> </a>" 
} 
+0

謝謝..它的工作..所以樣式不是在Android端渲染..只有屬性。 –

+0

不客氣。我很高興我幫助:) – Laura