WebView 未在 AlerDialog 中显示
我正在尝试在“AlertDialog”中显示“WebView”。 为此,我提到了此处发布的问题之一:在 AlertDialog 中显示 WebView 它成功打开了对话框窗口。但我不知道它为什么不显示网页内容。 这是我的“print_webview”文件,我在 AlertDialog 窗口中膨胀该文件:
'<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root">
<WebView
android:id="@+id/dialog_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>'
这是我的 Java 文件:
'public class CloudPrintTest extends Activity {
static final int GOOGLE_CLOUD_PRINT_DIALOG = 1; //dialog ID for google cloud print
/** Called when the activity is first created. */
Button printButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
printButton = (Button)findViewById(R.id.button1);
printButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//startActivity(new Intent(CloudPrintTest.this,PrintDialog.class));
showDialog(GOOGLE_CLOUD_PRINT_DIALOG);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case GOOGLE_CLOUD_PRINT_DIALOG:
//LayoutInflater layoutInflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//final View dialogLayout = layoutInflator.inflate(R.layout.print_webview, null);
LayoutInflater inflater = LayoutInflater.from(CloudPrintTest.this);
View alertDialogView = inflater.inflate(R.layout.print_webview, null);
WebView myWebView = (WebView)alertDialogView.findViewById(R.id.dialog_webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setWebViewClient(new PrintTestWebViewClient());
myWebView.loadUrl("http://www.google.com/");
AlertDialog.Builder builder = new AlertDialog.Builder(CloudPrintTest.this);
builder.setView(alertDialogView);
builder.setTitle("Google Cloud Print");
builder.setCancelable(true);
AlertDialog printDialog = builder.create();
return printDialog;
}
return null;
}
private class PrintTestWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}'
我已经尝试了所有可能的方法,但没有得到结果。 请帮忙。 问候
I am trying to display 'WebView' in 'AlertDialog'.
For that I referred one of the question posted here: Displaying WebView in AlertDialog
It successfully opens the dialog window. But I dont know some how its not showing the web content.
This is my 'print_webview' file, which I am inflating in AlertDialog window:
'<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root">
<WebView
android:id="@+id/dialog_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>'
This is my Java file:
'public class CloudPrintTest extends Activity {
static final int GOOGLE_CLOUD_PRINT_DIALOG = 1; //dialog ID for google cloud print
/** Called when the activity is first created. */
Button printButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
printButton = (Button)findViewById(R.id.button1);
printButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//startActivity(new Intent(CloudPrintTest.this,PrintDialog.class));
showDialog(GOOGLE_CLOUD_PRINT_DIALOG);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case GOOGLE_CLOUD_PRINT_DIALOG:
//LayoutInflater layoutInflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//final View dialogLayout = layoutInflator.inflate(R.layout.print_webview, null);
LayoutInflater inflater = LayoutInflater.from(CloudPrintTest.this);
View alertDialogView = inflater.inflate(R.layout.print_webview, null);
WebView myWebView = (WebView)alertDialogView.findViewById(R.id.dialog_webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setWebViewClient(new PrintTestWebViewClient());
myWebView.loadUrl("http://www.google.com/");
AlertDialog.Builder builder = new AlertDialog.Builder(CloudPrintTest.this);
builder.setView(alertDialogView);
builder.setTitle("Google Cloud Print");
builder.setCancelable(true);
AlertDialog printDialog = builder.create();
return printDialog;
}
return null;
}
private class PrintTestWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}'
I have tried all the possible ways, but didnt get the result.
Please help.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你创建一个活动。
当您在清单中注册它时。给它一个对话框外观
What i would suggest is you creating an activity.
And when you register it in your manifest. Give it the Dialog look with