I wanna set maximum height of dialog. Not custom height like that set by dp or px. I wanna set the greatest possible height to dialog relatively current device screen size.
You can not set max height directly. Just an alternate, you can reset height if its height is greater than maximum height you want to set.
If your dialog is made out of a vertical LinearLayout, just add a "height filling" dummy view, that will occupy the entire height of the screen. Nice!
jQuery: Calculate max height and apply to all element using jQuery. Here equalheight is parent class to find intro class and set equal height to all. maxHeight = $(this).
may be addOnLayoutChangeListener() is usefull for you.
you can add it before or after dialog.show()
this is my code :
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View view = LayoutInflater.from(this).inflate(R.layout.custom_alertdialog, null);
builder.setMessage("TestMessage xxxxxxx");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view); //在setView之前调用builder的原有设置控件方法时,能展示设置的控件,之后设置的则不展示!!
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_bk_cnoneralert));
//builder.show(); //用这个的话,背景并不会改变,依旧是默认的
dialog.show(); //必须用这个show 才能显示自定义的dialog window 的背景
//这种设置宽高的方式也是好使的!!!-- show 前调用,show 后调用都可以!!!
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
int oldRight, int oldBottom) {
int height = v.getHeight();
int contentHeight = view.getHeight();
LogUtils.e("高度", height + " / " + " / " + contentHeight);
int needHeight = 500;
if (contentHeight > needHeight) {
//注意:这里的 LayoutParams 必须是 FrameLayout的!!
//NOTICE : must be FrameLayout.LayoutParams
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
needHeight));
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With