java - 在 Android 中的 Fragment 中使用 WebView 时,页面显示未格式化的文本

标签 java android android-layout android-fragments

设法让一些事情发挥作用。我正在开发的应用程序显示一个 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/

相关文章:

Android:向 ListView App Widget 添加多个 View

Android CalendarView 减慢布局

java - 在持续集成服务器上使用 logback 和 TestNG 的良好实践

java - 是否可以将自定义 list 添加到在 Netbeans 6.7.1 中编译的 Java 库?

java - EntityManagerFactory 关闭,Hibernate

java - 如何防止在 JTable 单元格中输入数字以外的字符?

java - 从 Android 设备调用 .Net 网络服务

c# - Visual Studio 无法安装 Android SDK(API 级别 19 和 21)(缺少 extra-android-support)

java - 如何用 ImageViews 替换 youtube iframe 标签或缩略图

android - 是否使用自定义图标字体将 ImageView/ImageButton(为图标设置)替换为 Textview 良好做法?