无法设置 AlertDialog 的大小 Android
Can't set size of AlertDialog Android
在方法onOptionsItemSelectedMenu
中我有一个AlertDialog.Builder
我正在这个 AlertDialog 中打开一个 WebView。
但是,我想改变AlertDialog.Builder的大小,我想根据我的decision.I改变宽度和高度,在这里看了很多问题,但奇怪的是没有任何效果。特别是这篇文章:How to make an alert dialog fill 90% of screen size?
我怎样才能改变它的大小?我想以编程方式更改它。我不会膨胀任何布局或其他任何东西。
这是我的小代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_inner_basics) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
android.webkit.WebView wv = new WebView(this);
wv.loadUrl("file:///android_asset/test.html");
AlertDialog alertDialog = alert.create();
alertDialog.setView(wv);
alertDialog.show();
}
return super.onOptionsItemSelected(item);
}
一种可能的方法是在新的 activity 中显示您的内容,样式为 Theme.AppCompat.Light.Dialog
您必须在调用 Dialog.show()
后调整对话框的大小。我通常创建一个新的 class extends from AppCompatDialog
用于对话框。在下面的代码中,我覆盖了 show()
方法并将 resizeDialog();
放在 show()
方法的末尾。
public class LocationDialog extends AppCompatDialog {
public LocationDialog(Activity activity) {
super(activity);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
initView();
}
private void initView(){
// To get WebView from dialog_layout.xml
// And showing website in this view
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
}
@Override
public void show() {
super.show();
resizeDialog();
}
/**
* To resize the size of this dialog
*/
private void resizeDialog() {
try {
Window window = getWindow();
if (activity == null || window == null) return;
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
window.setLayout((int) (width * 0.95), (int) (height * 0.85));
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// make tranparent around the popup
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
在你的 activity 中,你应该这样做:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_inner_basics) {
LocationDialog dialog = new LocationDialog(Activity);
dialog.show();
}
return super.onOptionsItemSelected(item);
}
方法如下!由于 AlertBuilder
将始终采用给定的整个 View
因此让我们首先获取设备大小,以便可以不随设备大小而变化。此方法将 return 设备大小表示为 int
的 array
。在你的 activity
.
中定义它
private int[] getScreenSIze(){
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
int w = displaymetrics.widthPixels;
int[] size={w,h};
return size;
}
如果您是一个片段,请考虑将方法中的一行更改为
getActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
如果您在 activity 中,请忽略该行。
并且当用户单击选择的选项项时我们将 webview
的高度和宽度设置为设备高度的 70% 和设备宽度的 80%,因为 webview
处于警报状态生成器。 Alert Builder 将 webView
扩展覆盖如下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_inner_basics) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int[] screenSize= getScreenSIze(); //This is the method above remember
int width=screenSize[0];
int height=screenSize[1];
int webViewWidth=(int)(0.8*width); //Casting to make them int
int webViewHeight=(int)(0.7*height);
android.webkit.WebView wv = new WebView(this);
wv.setMinimumHeight(webViewHeight);
wv.setMinimumWidth(webViewWidth);
wv.loadUrl("file:///android_asset/test.html");
builder.setView(wv);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
return super.onOptionsItemSelected(item);
}
我希望它能工作,你可以通过更改 0.7 和 0.8 来控制你希望出现的高度和宽度。编码愉快!
在方法onOptionsItemSelectedMenu
中我有一个AlertDialog.Builder
我正在这个 AlertDialog 中打开一个 WebView。
但是,我想改变AlertDialog.Builder的大小,我想根据我的decision.I改变宽度和高度,在这里看了很多问题,但奇怪的是没有任何效果。特别是这篇文章:How to make an alert dialog fill 90% of screen size? 我怎样才能改变它的大小?我想以编程方式更改它。我不会膨胀任何布局或其他任何东西。
这是我的小代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_inner_basics) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
android.webkit.WebView wv = new WebView(this);
wv.loadUrl("file:///android_asset/test.html");
AlertDialog alertDialog = alert.create();
alertDialog.setView(wv);
alertDialog.show();
}
return super.onOptionsItemSelected(item);
}
一种可能的方法是在新的 activity 中显示您的内容,样式为 Theme.AppCompat.Light.Dialog
您必须在调用 Dialog.show()
后调整对话框的大小。我通常创建一个新的 class extends from AppCompatDialog
用于对话框。在下面的代码中,我覆盖了 show()
方法并将 resizeDialog();
放在 show()
方法的末尾。
public class LocationDialog extends AppCompatDialog {
public LocationDialog(Activity activity) {
super(activity);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
initView();
}
private void initView(){
// To get WebView from dialog_layout.xml
// And showing website in this view
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
}
@Override
public void show() {
super.show();
resizeDialog();
}
/**
* To resize the size of this dialog
*/
private void resizeDialog() {
try {
Window window = getWindow();
if (activity == null || window == null) return;
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
window.setLayout((int) (width * 0.95), (int) (height * 0.85));
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// make tranparent around the popup
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
在你的 activity 中,你应该这样做:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_inner_basics) {
LocationDialog dialog = new LocationDialog(Activity);
dialog.show();
}
return super.onOptionsItemSelected(item);
}
方法如下!由于 AlertBuilder
将始终采用给定的整个 View
因此让我们首先获取设备大小,以便可以不随设备大小而变化。此方法将 return 设备大小表示为 int
的 array
。在你的 activity
.
private int[] getScreenSIze(){
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
int w = displaymetrics.widthPixels;
int[] size={w,h};
return size;
}
如果您是一个片段,请考虑将方法中的一行更改为
getActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
如果您在 activity 中,请忽略该行。
并且当用户单击选择的选项项时我们将 webview
的高度和宽度设置为设备高度的 70% 和设备宽度的 80%,因为 webview
处于警报状态生成器。 Alert Builder 将 webView
扩展覆盖如下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_inner_basics) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int[] screenSize= getScreenSIze(); //This is the method above remember
int width=screenSize[0];
int height=screenSize[1];
int webViewWidth=(int)(0.8*width); //Casting to make them int
int webViewHeight=(int)(0.7*height);
android.webkit.WebView wv = new WebView(this);
wv.setMinimumHeight(webViewHeight);
wv.setMinimumWidth(webViewWidth);
wv.loadUrl("file:///android_asset/test.html");
builder.setView(wv);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
return super.onOptionsItemSelected(item);
}
我希望它能工作,你可以通过更改 0.7 和 0.8 来控制你希望出现的高度和宽度。编码愉快!