【问题标题】:I want to Show Static HTML files from Assets folder into Webview in Custom Dialog我想在自定义对话框中将 Assets 文件夹中的静态 HTML 文件显示到 Webview 中
【发布时间】:2012-10-27 03:07:03
【问题描述】:

我正在尝试将资产文件夹中的 html 文件显示到自定义对话框中。但得到错误。 我正在使用以下代码。

我的自定义对话框类

public class RobozoxDialog extends Activity{
Dialog my;
public RobozoxDialog(Context theContext){
    my= new Dialog(theContext);
}
public void showRobozoxDialog(){
    my.show();
}
public void setSource(String title,String url) {

    WebView myWebView = (WebView) findViewById(R.id.webview_dialog);
    myWebView.loadUrl(url); 
    my.setTitle(title);
}
}

自定义对话框 XML

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>

像这样调用对话框。

RobozoxDialog d = new RobozoxDialog(MainActivity.this);
d.setSource("Test", "file:///android_asset/test.html");
d.showRobozoxDialog();

但出现错误并无法解决问题。当我在主要活动 xml 的 webview 中显示 html 文件时,它正在显示。但是当尝试在对话框 webview 中显示时它停止工作..

【问题讨论】:

    标签: android


    【解决方案1】:

    我通过使用 LayoutInflater 来完成此操作,代码如下..

        LayoutInflater li = LayoutInflater.from(theContext);
        View promptsView = li.inflate(R.layout.dialog_layout, null);
    
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                theContext);
        alertDialogBuilder.setTitle("Registration");
    
        alertDialogBuilder.setView(promptsView);
    
        final WebView webview = (WebView) promptsView
                .findViewById(R.id.webview_dialog);
        webview.loadUrl("file:///android_asset/test.html");
        final AlertDialog alertDialog = alertDialogBuilder.create();
    
        alertDialog.show();
    

    【讨论】:

      【解决方案2】:
      1. 始终将 logcat 输出添加到您的问题中。我们不知道您遇到了什么错误。
      2. 您从未为对话框设置任何布局。请参阅here 如何创建自定义对话框。
      3. 您尝试从 Activity 而不是 Dialog 获取 WebView。试试my.findViewById(R.id.webview_dialog)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-24
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        • 2019-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多