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

控件如下

    <LinearLayout
        android:id="@+id/devicelist_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
     </LinearLayout>

写一个类

    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

发布了305 篇原创文章 · 获赞 261 · 访问量 184万+

猜你喜欢

转载自blog.csdn.net/chenguang79/article/details/94553946