alertdialog 设置最大高度

时间:2021-07-25 08:10:32

设置最大高度,有很多方法,我个人比较喜欢的是下面这种方式 ,这里的view即添加到

  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 = view.getHeight();
int maxHeight = DensityUtils.dpToPx(mHomeActivity, 200);
if(height> maxHeight){
            //这里也可采用现获取layoutParams
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,maxHeight));
//这里一定是frameLayout,不然会转换异常,由此可见alertdialog底层是frameLayout
} }
});

2.在dialog中含有recyclerView或者listview,用的relativeLayout,结果listview下面会被截取掉,这里应该这么处理

 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 = mDeviceList.getHeight();
int maxHeight = DensityUtils.dpToPx(mHomeActivity, 200); if(height> maxHeight){
//必须首先获取控件的layoutParams,然后再更改,new一个不行
ViewGroup.LayoutParams layoutParams = mDeviceList.getLayoutParams();
layoutParams.height = maxHeight;
mDeviceList.setLayoutParams(layoutParams);
} }
});

这种方法会导致recyclerview的点击事件,不能及时响应。原因暂时不明