我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
其他回答
在自定义视图xml中设置android:minWidth和android:minHeight。这可以强制警告不只是包装内容大小。 使用这样的视图应该做到:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="300dp"
android:minHeight="400dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/icon"/>
</LinearLayout>
public static WindowManager.LayoutParams setDialogLayoutParams(Activity activity, Dialog dialog)
{
try
{
Display display = activity.getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int width = screenSize.x;
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = (int) (width - (width * 0.07) );
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
return layoutParams;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
final AlertDialog alertDialog;
LayoutInflater li = LayoutInflater.from(mActivity);
final View promptsView = li.inflate(R.layout.layout_dialog_select_time, null);
RecyclerView recyclerViewTime;
RippleButton buttonDone;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
alertDialogBuilder.setView(promptsView);
// create alert dialog
alertDialog = alertDialogBuilder.create();
/**
* setting up window design
*/
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
DisplayMetrics metrics = new DisplayMetrics(); //get metrics of screen
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = (int) (metrics.heightPixels * 0.9); //set height to 90% of total
int width = (int) (metrics.widthPixels * 0.9); //set width to 90% of total
alertDialog.getWindow().setLayout(width, height); //set layout
recyclerViewTime = promptsView.findViewById(R.id.recyclerViewTime);
DialogSelectTimeAdapter dialogSelectTimeAdapter = new DialogSelectTimeAdapter(this);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerViewTime.setLayoutManager(linearLayoutManager);
recyclerViewTime.setAdapter(dialogSelectTimeAdapter);
buttonDone = promptsView.findViewById(R.id.buttonDone);
buttonDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
你必须在显示之前设置对话框的高度和宽度(dialog.show())
所以,像这样做:
dialog.getWindow().setLayout(width, height);
//then
dialog.show()
到目前为止我能想到的最简单的方法
如果你的对话框是由垂直线性布局组成的,只需添加一个“高度填充”虚拟视图,它将占据屏幕的整个高度。
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editSearch" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
<!-- this is a dummy view that will make sure the dialog is highest -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
注意LinearLayout属性中的android:weightSum="1"和dummy View属性中的android:layout_weight="1"