In the long years of development, have you ever run into a problem where we set the wrap_contant property to a layout height, but somewhere we need to get an actual height for it, what do we do? Here is a way to get the height of a View.
public static float getRealHeight(View child){ LinearLayout llayout = (LinearLayout) child; ViewGroup.LayoutParams params = llayout.getLayoutParams(); if (params == null){ params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int widthSpec = ViewGroup. GetChildMeasureSpec (0, 0, params. Width); int heightSpec; if (params.height>0){ heightSpec = View.MeasureSpec.makeMeasureSpec(params.height, View.MeasureSpec.EXACTLY); }else { heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); } llayout.measure(widthSpec,heightSpec); return llayout.getMeasuredHeight(); // To get the height of the layout, call getMeasuredWidth()}Copy the code
So we get a height of the view, and we can change the width of the return, and we can change the width of the view.