[英]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.