【问题标题】:Android best way to display html textAndroid显示html文本的最佳方式
【发布时间】:2018-06-03 18:40:09
【问题描述】:

我有带有html tagslinksimage links 的字符串,那么显示此字符串的最佳方式是什么?我知道人们为此使用Webview,但也许有一种方法可以在 textview 中做到这一点而不需要太多工作?因为WebView 会带来不同的问题,例如,如果您想更改文本颜色,则需要为该字符串添加额外的样式。我对使链接可点击并在同一文本视图中显示图像的方法感兴趣。

【问题讨论】:

  • 您将在 HTML 中使用的完整标签列表是什么?
  • 它不是重复的,你看我知道如何在 textview 中显示 html,但是如何使用 textview 使链接和使它们可点击,以及在这个 textview 中显示来自链接的图像

标签: android webview textview


【解决方案1】:

Try using this:-

Html.fromHtml("your html code");

Example:-

txtvw.setText(Html.fromHtml("<p align=right> <b> "
        + "Hi!" + " <br/> <font size=6>"
        + " How are you "+"</font> <br/>"
        + "I am fine" + "  </b> </p>"));

Output:-


你好吗
我很好

**Full Code With Image And Hyperlink**:-

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class MainActivity extends Activity {

 String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='https://stackoverflow.com/'>Stack Overflow</a></b>";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TextView htmlTextView = new TextView(this);
  setContentView(htmlTextView);

  htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter(){

   @Override
   public Drawable getDrawable(String source) {
    Drawable drawable;
    int dourceId = 
      getApplicationContext()
      .getResources()
      .getIdentifier(source, "drawable", getPackageName());

    drawable = 
      getApplicationContext()
      .getResources()
      .getDrawable(dourceId);

    drawable.setBounds(
      0, 
      0, 
      drawable.getIntrinsicWidth(),
      drawable.getIntrinsicHeight());

    return drawable;
   }

  }, null));

  htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());

 }

}

To support all API use this function:-

@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
    }
    else {
        return Html.fromHtml(html);
    }
}

【讨论】:

  • 如何让链接可点击或显示图片?
  • 不错!我猜 27 api fromHtml 的另一件事已被弃用,这种方法是否适用于 27 及更高版本的 api?
【解决方案2】:

它只对我有用的方式是:

class HelpActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.help_activity)
        val txtView: TextView = findViewById(R.id.help_textView)
        val str = resources.getString(R.string.help_textview)
        txtView.text = Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT)
    }
}

字符串资源使用 CDATA 包装器:

]]>

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2014-10-16
    • 2010-11-24
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多