控件如下
<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));