android中设置一些没有maxHeight属性控件的最高值

控件如下

    
     

写一个类

    public class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
        private int maxHeight = 500;
        private View view;
     
        public OnViewGlobalLayoutListener(View view, int height) {
            this.view = view;
            this.maxHeight = height;
        }
     
        @Override
        public void onGlobalLayout() {
            if (view.getHeight() > maxHeight)
                view.getLayoutParams().height = maxHeight;
        }
    }

设置如下:

    View view = findViewById(R.id.devicelist_layout);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new OnViewGlobalLayoutListener(view, 300));

转自:https://blog.haloxin.me/post/100.html

你可能感兴趣的:(Android)