设法让一些事情发挥作用。我正在开发的应用程序显示一个 Web View ,就像我希望它在 fragment 中一样,但该 fragment 只显示未格式化的纯文本,无论我在字符串中放入什么 HTML 样式(尽管文本是正确的)。有什么想法我做错了什么吗?
fragment (忽略文件的命名,将被修复,如textviewabout):
public static class AboutMeFragment extends Fragment {
public AboutMeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.aboutme,
container, false);
WebView webView = (WebView) rootView.findViewById(R.id.textviewabout);
String summary = (getString(R.string.about_me));
webView.loadData(summary, "text/html", null);
return rootView;
}
}
在字符串中:
<string name="about_me">"<html><body><b>This is a test of bold</b></body></html>"</string>
以及关于我.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$AboutMeFragment" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/textviewabout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:scrollbars = "vertical" />
</ScrollView>
</RelativeLayout>
最佳答案
如果你看一下Resources在文档中,您将看到 getString
方法从字符串中删除格式信息。由于 Android 将 HTML 视为样式信息。如果您想从字符串文件返回 HTML,您将需要执行以下操作;
<string name="about_me"><![CDATA[
<html><body><b>This is a test of bold</b></body></html>
]]></string>
Android 不会从您的文本中去除 HTML,您应该会得到您想要的粗体 HTML。
关于java - 在 Android 中的 Fragment 中使用 WebView 时,页面显示未格式化的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22429485/