Android Web View 不在某些手机上显示加载的 Html 内容,特别是 htc one x

标签 android android-layout android-intent android-listview android-webview

我正在 WebView 中使用 CSS 加载一些 Html 内容。
在每部手机上一切正常,但 HTC one x(安卓版本 4.1.1)。
此手机上的 WebView 中未显示任何内容。
下面是我的代码 fragment 。

给我格式化的 WebView 内容的函数:

public String getFormattedWebViewContents(String content)
{       

    String htmlString = "ul{" +
                "padding-left: 10px;" +
                "padding-bottom:7px;" +
                "color:#818181;" +
                "font-size: 10px;font-family: Verdana, Arial, Helvetica, sans-serif; }" +
                "ul .bdr { line-height: 16px;}" + 
                ".flt-lt{float:left;}"   + 
                ".flt-rt{float:right;}"  +
                "ul li{padding:10px 5px 10px 10px !important;}"+
                "ul li h4 {height: 25px; border: 1px solid #DEDEDE; font-family:Calibri;font-size:18px;" + 
                "margin-bottom:10px !important;font-weight:bold;text-align: left; vertical-align: middle;margin-left: -7px;" +
                "margin-right: 0px !important;" +
                "color: #7CA834;" +
                "font-family: Calibri;" +
                "font-size: 18px;" +
                "font-weight: bold;" +
                "height: 25px;" +
                "margin-bottom: 1px;" +
                "padding-left: 10px;" +
                "padding-top: 10px !important;" +
                "text-align: left;"+
                "vertical-align: middle;"+                  
                "background-color: #F2F2F2;color: #7CA834;}"+
                ".bdr1 .sml-font{font-size:11px !important;padding-top:3px ;color: #7CA834 !important;font-family: Arial !important;font-weight: bold !important;}"+
                "ul{margin-bottom:14px; margin-top:12px;background-color: #ffffff; padding-left:10px; padding-right:0px; padding-top:10px; }"+
                "ul{padding-left: 10px;padding-top:8px;padding-bottom:7px;color: #818181;font-size:10px;font-family: Verdana, Arial, Helvetica, sans-serif;}"+
                "#factstable td{ width:170px; height:18px;}"+
                ".ext { float:right; }"+
                ".factsdetaildiv{border: 1px solid #DEDEDE;padding-left: 10px;padding-bottom:7px;color:#818181;font-size: 10px;font-family: Verdana, Arial, Helvetica, sans-serif; }"+
                "ul .bdr { line-height: 16px;}"+
                "ul{color:#818181;font-size: 10px;font-family: Verdana, Arial, Helvetica, sans-serif; }"+
                "ul{list-style-type:none;margin-right: 0px; padding:10px;}"+
                "ul li{padding-bottom:7px; padding-top:10px !important;line-height:10PX; list-style-image:none;font-size:11px;font-family:Arial, Helvetica, sans-serif;}"+
                "ul{ font-weight:bold;}"+
                "ul p{ padding:0px 10px 0px 0px;line-height: 12px;color: #818181;font-size: 12px;font-family: Arial;}" +
                " li:first-child { margin-top:-38px !important; padding:10px 5px 10px 10px;}"
                ;

      return "<html>" + "<head>" + "<style type=\"text/css\"> " + "@font-face " + "{ " + "font-family: MyCustomFont; " + "src: url(\"file:///android_asset/font/" + mContext.getResources().getString(R.string.type_face_regular) + "\") " + "}" + "body " + "{ " + "font-family: MyCustomFont; " + "font-size: "+(Utility.isLargeScreen(mContext)? "22px; ": "18px; ") + "color: black" + "}" + htmlString + "</style>" + "</head><body style='margin: 10px'>" + content + "</body></html>";


}

我的 WebView :

mDetailsContentsWebView.setBackgroundColor(getResources().getColor(R.color.color_product_details_webview_bg));
        mDetailsContentsWebView.loadDataWithBaseURL("", Utility.getInstance(getActivity()).getFormattedWebViewContents(mCurrentProductDetails.mDescription), "text/html", "UTF-8", "");
        mDetailsContentsWebView.getSettings().setSupportZoom(false);
        mDetailsContentsWebView.getSettings().setJavaScriptEnabled(true);
        mDetailsContentsWebView.setOnTouchListener(new View.OnTouchListener()
        {
            public boolean onTouch(View v, MotionEvent event)
            {
                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                    {
                    }
                        break;

                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                    {
                        event.setLocation(event.getX(), event.getY());
                    }
                        break;

                }
                return false;
            }
        });

如果 HTC 手机有问题或我的代码 fragment 有误,请有人告诉我。
谢谢。

最佳答案

问题在行

mDetailsContentsWebView.loadDataWithBaseURL("", Utility.getInstance(getActivity()).getFormattedWebViewContents(mCurrentProductDetails.mDescription), "text/html", "UTF-8", "");

您正在使用 ""代替 baseURL。尝试指定任何有效的 URL 或任何本地文件路径(来自 Assets )。

或者将这一行替换为

mDetailsContentsWebView.loadData(Utility.getInstance(getActivity()).getFormattedWebViewContents(mCurrentProductDetails.mDescription), "text/html", "UTF-8");

关于Android Web View 不在某些手机上显示加载的 Html 内容,特别是 htc one x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621657/

相关文章:

java - ListView 仅显示 ListView 第一个位置的最后一项

android - 由于窗口没有焦点而丢弃事件

java - ArrayList<ParseQuery> 不确定如何使用?

android - 如何以编程方式访问 Android 11 中的隐藏文件?

android - 来自 "Resent apps"时未更新 Intent

android - 设置抽屉导航图标的背景

android - 如何更改复选框的颜色?

使用 TOP 和 BOTTOM 重力时 Android DrawerLayout 失败

android - AppWidget setFillInIntent无法处理setData或componentName中设置的数据?

android - 无法在 Intent : The Method Put Extra is Ambiguous for the type Intent 中传递自定义对象