繁体   English   中英

在对话框中加载Web视图时出现问题

[英]Issue Loading a webview within a dialog

我正在尝试在对话框中加载Web视图,但Web视图从不显示,我已将html文件存储在原始文件夹中。 我只会看到一个带有标题的对话框。任何建议都会有所帮助。

public class MainActivity extends Activity
 {
    String linewise="";
    final Context context=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

    Button button=(Button)findViewById(R.id.button);    


    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final Dialog dialog=new Dialog(context);
            dialog.setContentView(R.layout.dialog_webview);
            dialog.setTitle("WebView");

            WebView webview=(WebView)dialog.findViewById(R.id.webview);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl("file:///android_res/raw/abc.htm"); 
            dialog.show();  


        }


    });
    }   

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView 
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

这是我在对话框中加载Webview的代码,对我来说很好。 将您的html文件放入资产文件夹中。

         AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
         alert.setTitle("Title here");

            WebView wv = new WebView(MainActivity.this);
            wv.getSettings().setJavaScriptEnabled(true);
            wv.loadUrl("file:///android_asset/myhtml.html");
            alert.setView(wv);
            alert.setNegativeButton("Close",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });


         alert.show();

请接受并尝试此代码。它绝对适合您。

将layout_height更改为wrapcontent可以解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM