如何在 android 文本视图中显示带有图像和富文本的段落。内容可以存储在 xml
How to display a paragraph with images and rich text in android text view. Content can be stored in xml
我有一些包含图像和富文本格式的内容,以及应该在 android 中工作的链接。如何在 android 文本视图中显示它们。以及我应该在哪里存储内容。最佳做法是什么?使用 HTML 内容或将其拆分为单独的文本和图像视图?
例如,我想制作如下图所示的文本视图。
使用 HTML 并将其显示在 android 网络视图中,而不是文本视图中。
String text = textView.getText().toString();
SpannableString spannableString = new SpannableString(text);
IconFontSpan iconFontSpan = new IconFontSpan(textView.getContext());
Pattern pattern = Pattern.compile("\u26F7"); // skier
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
spannableString.setSpan(iconFontSpan,
matcher.start(), matcher.end(), 0);
}
private static class IconFontSpan
extends MetricAffectingSpan {
private static Typeface typeface = null;
public IconFontSpan(Context context) {
if (typeface == null) {
typeface = Typeface.createFromAsset(
context.getAssets(), "icomoon.ttf");
}
}
public void updateMeasureState(TextPaint textPaint) {
textPaint.setTypeface(typeface);
}
public void updateDrawState(TextPaint textPaint) {
textPaint.setTypeface(typeface);
}
这里是 link:
您应该将 HTML 和所有内容存储到资产文件夹中。
然后在 WebView 中加载 html。
在您的布局文件中写入此代码。
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
并且从您的 Activity 您可以像这样设置 html。
WebView webView= (WebView) findViewById(R.id.movieLoadingLoader);
webView.loadUrl("file:///android_asset/your_html_folder/index.html");
或者,
如果您想在 TextView 中加载 HTML,请编写此代码。
Textview tv = (TextView) findViewById(R.id.textView);
tv.setText(Html.fromHtml("<h4>Hello World</h4>"));
我有一些包含图像和富文本格式的内容,以及应该在 android 中工作的链接。如何在 android 文本视图中显示它们。以及我应该在哪里存储内容。最佳做法是什么?使用 HTML 内容或将其拆分为单独的文本和图像视图?
例如,我想制作如下图所示的文本视图。
使用 HTML 并将其显示在 android 网络视图中,而不是文本视图中。
String text = textView.getText().toString();
SpannableString spannableString = new SpannableString(text);
IconFontSpan iconFontSpan = new IconFontSpan(textView.getContext());
Pattern pattern = Pattern.compile("\u26F7"); // skier
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
spannableString.setSpan(iconFontSpan,
matcher.start(), matcher.end(), 0);
}
private static class IconFontSpan
extends MetricAffectingSpan {
private static Typeface typeface = null;
public IconFontSpan(Context context) {
if (typeface == null) {
typeface = Typeface.createFromAsset(
context.getAssets(), "icomoon.ttf");
}
}
public void updateMeasureState(TextPaint textPaint) {
textPaint.setTypeface(typeface);
}
public void updateDrawState(TextPaint textPaint) {
textPaint.setTypeface(typeface);
}
这里是 link:
您应该将 HTML 和所有内容存储到资产文件夹中。 然后在 WebView 中加载 html。 在您的布局文件中写入此代码。
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
并且从您的 Activity 您可以像这样设置 html。
WebView webView= (WebView) findViewById(R.id.movieLoadingLoader);
webView.loadUrl("file:///android_asset/your_html_folder/index.html");
或者,
如果您想在 TextView 中加载 HTML,请编写此代码。
Textview tv = (TextView) findViewById(R.id.textView);
tv.setText(Html.fromHtml("<h4>Hello World</h4>"));