LinearLayout自适应高度wap_content时如何设置最大高度
如题,如何设置下列LinearLayout的最大高度呢?
<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));
这样就可以设置LinearLayout的最大高度了
发布评论
热门评论区: