让 Webview 在 Android 中占据整个对话框 window
Make Webview occupy whole dialog window in Android
我的应用程序中有一个对话框片段,我想占据整个屏幕。因为我需要我的片段来扩展我的自定义布局,并想动态地向这个对话框添加视图,所以我编写了如下代码:-
my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyDialogRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dialogRootLayout"
android:orientation="vertical"
android:background="#FFFFFF"
>
</com.example.MyDialogRelativeLayout>
现在在我的对话框中 java class 我写道:-
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment {
WebView webView;
MyDialogRelativeLayout dialogLayout;
String url;
public MyDialogFragment(String url)
{
this.url = url;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.my_custom_dialog,container, false);
dialogLayout = (MyDialogRelativeLayout) rootView.findViewById(R.id.dialogRootLayout);
return rootView;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
Log.d("Dialog","Height = "+height+" Widht = "+width);
getDialog().getWindow().setLayout(width-50,height);
RelativeLayout.LayoutParams paramsWeb = new RelativeLayout.LayoutParams(width,height);
final WebView wv = new WebView(getActivity());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
wv.loadUrl("file:///android_res/raw/nointernet.html");
}
});
dialogLayout.addView(wv,paramsWeb);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Button closeButton = new Button(getActivity());
closeButton.setText("Close");
closeButton.setBackground(null);
dialogLayout.addView(closeButton,params);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
}
public class myWebViewClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
我的问题是,webview 没有占据绘制到屏幕上的整个对话框 window。 dialog上面有一个黑色的layer/area,下面是webview.
如何去除顶部的黑色部分,让 webview 填满整个对话框?
使用以下设置片段内的高度和宽度 class:getDialog().getWindow().setLayout(width, height);
要删除黑色部分(标题),请在您的片段中使用以下内容 class:getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
我的应用程序中有一个对话框片段,我想占据整个屏幕。因为我需要我的片段来扩展我的自定义布局,并想动态地向这个对话框添加视图,所以我编写了如下代码:-
my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyDialogRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dialogRootLayout"
android:orientation="vertical"
android:background="#FFFFFF"
>
</com.example.MyDialogRelativeLayout>
现在在我的对话框中 java class 我写道:-
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment {
WebView webView;
MyDialogRelativeLayout dialogLayout;
String url;
public MyDialogFragment(String url)
{
this.url = url;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.my_custom_dialog,container, false);
dialogLayout = (MyDialogRelativeLayout) rootView.findViewById(R.id.dialogRootLayout);
return rootView;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
Log.d("Dialog","Height = "+height+" Widht = "+width);
getDialog().getWindow().setLayout(width-50,height);
RelativeLayout.LayoutParams paramsWeb = new RelativeLayout.LayoutParams(width,height);
final WebView wv = new WebView(getActivity());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
wv.loadUrl("file:///android_res/raw/nointernet.html");
}
});
dialogLayout.addView(wv,paramsWeb);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Button closeButton = new Button(getActivity());
closeButton.setText("Close");
closeButton.setBackground(null);
dialogLayout.addView(closeButton,params);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
}
public class myWebViewClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
我的问题是,webview 没有占据绘制到屏幕上的整个对话框 window。 dialog上面有一个黑色的layer/area,下面是webview.
如何去除顶部的黑色部分,让 webview 填满整个对话框?
使用以下设置片段内的高度和宽度 class:getDialog().getWindow().setLayout(width, height);
要删除黑色部分(标题),请在您的片段中使用以下内容 class:getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);