我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
你必须在显示之前设置对话框的高度和宽度(dialog.show())
所以,像这样做:
dialog.getWindow().setLayout(width, height);
//then
dialog.show()
得到这段代码,我做了一些改变:
dialog.getWindow().setLayout((int)(MapGeaGtaxiActivity.this.getWindow().peekDecorView().getWidth()*0.9),(int) (MapGeaGtaxiActivity.this.getWindow().peekDecorView().getHeight()*0.9));
但是,当设备改变位置时,对话框的大小也会改变。当度量发生变化时,您可能需要自己处理。 peekDecorView,暗示在活动中的布局是正确初始化的,否则你可以使用
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;
为了得到屏幕大小
其他回答
***In Kotlin You can Code like This : -***
fun customDialog(activity: Activity?, layout: Int): Dialog {
val dialog = Dialog(activity!!)
try {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(layout)
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.window!!.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
dialog.show()
} catch (e: Exception) {
}
return dialog
}
在对话框窗口上指定FILL_PARENT,就像其他人建议的那样,对我来说并不管用(在Android 4.0.4上),因为它只是拉伸了黑色对话框背景来填充整个屏幕。
正常工作的方法是使用最小显示值,但在代码中指定它,以便对话框占用屏幕的90%。
So:
Activity activity = ...;
AlertDialog dialog = ...;
// retrieve display dimensions
Rect displayRectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
// inflate and adjust layout
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_dialog_layout, null);
layout.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
layout.setMinimumHeight((int)(displayRectangle.height() * 0.9f));
dialog.setView(layout);
一般来说,在大多数情况下,只调整宽度就足够了。
我发现了一个非常简单易行的解决办法
fun showDialog(){
val dialog = Dialog(this@DialogActivity)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.custom_dialog)
val txtTitle = dialog.findViewById<TextView>(R.id.txtTitle)
val btn = dialog.findViewById<Button>(R.id.button)
btn.setOnClickListener {
Toast.makeText(this,"test",Toast.LENGTH_SHORT).show()
}
txtTitle.setText("ali")
dialog.show()
val window = dialog.window
window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT)
}
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()