Application scenario: measure the width and height of view s that have not been added or rendered
For example, when the view is not drawn and displayed, get the height of the item view in the list, and a TextView needs the width after setting the text .
There are many ways to obtain the width and height of the View, most of which can be obtained after the View is drawn. This chapter is about directly measuring the size required by the view object.
Solution: Call the view.measure() method to measure its width and height
Step 1: Get the view object
Step 2: Set the width and height properties (the size mode must be set)
Remarks: The mode must be set View.MeasureSpec.EXACTLY or View.MeasureSpec.AT_MOST , otherwise the default is View.MeasureSpec.EXACTLY
The mode is not set correctly, it will affect the measurement results
View.MeasureSpec.EXACTLY is equivalent to match_parent, View.MeasureSpec.EXACTLY is equivalent to wrap_content
For example, set the height to warp_content, then set the height as follows
//Set the height of textView to wrap_content (-2) and the mode to View.MeasureSpec.AT_MOST int h = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.AT_MOST);
If the width is match_parent, you need to confirm the maximum size of the parent control. The following is the screen width
//Set the width of the textView to the screen width and the mode to macth_parent (View.MeasureSpec.EXACTLY)
int w = View.MeasureSpec.makeMeasureSpec(ScreenUtils.getScreenWidth(), View.MeasureSpec.EXACTLY);
Note: If you need to test the view, its size is match_parent, or it has attributes such as weight, padding,margin, etc., and the width and height attributes cannot be simply determined, you can choose to measure its parent control, and then use the view object to obtain the measurement size, the same It is possible to get the measured width and height values (you can learn the measurement process of the view).
The third step: first call the view.measure(width,heigth) method, and then call the method to obtain the measurement width and height
tv.measure(w,h); int measuredWidth = tv.getMeasuredWidth();//Measured width 1080px int measuredHeight = tv.getMeasuredHeight();//Measured height 137px
Application Scenario 1: Measure the height required by TextView after setting the text
code show as below:
final TextView tv = new TextView(this);//new a TextView object String testStr = "H"; String str = getNewStr(testStr, 100);//Get 100 "H" strings tv.setText(str);//set text int screenWidth = ScreenUtils.getScreenWidth(); int width = screenWidth / 2; int wrapContent = ViewGroup.LayoutParams.WRAP_CONTENT; // int measuredWidth = tv.getMeasuredWidth();//Before the measurement method is called, the measured width and height are 0 // int measuredHeight = tv.getMeasuredHeight();// == 0 //Set the width of the textView to the screen width and the mode to macth_parent (View.MeasureSpec.EXACTLY) int w = View.MeasureSpec.makeMeasureSpec(ScreenUtils.getScreenWidth(), View.MeasureSpec.EXACTLY); //Set the height of the textView to the screen wrap_content (-2) and the mode to View.MeasureSpec.AT_MOST int h = View.MeasureSpec.makeMeasureSpec(wrapContent, View.MeasureSpec.AT_MOST); //Set the layoutParams property of textView ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(screenWidth, wrapContent); tv.setLayoutParams(layoutParams); //Measure width and height The incoming width and height must be set to the mode, otherwise it is the default mode tv.measure(w,h); int lineCount = tv.getLineCount();//3 lines int measuredWidth = tv.getMeasuredWidth();//Measure width 1080px int measuredHeight = tv.getMeasuredHeight();//Measured height 137px String newStr = getNewStr(testStr, 200);//Get 200 "H" strings tv.setText(newStr);//Get 200 "H" strings tv.measure(w,h);//measure again int measuredWidth1 = tv.getMeasuredWidth();//Measure width 1080px int measuredHeight1 = tv.getMeasuredHeight();//Measured height measuredHeight:223px int lineCount1 = tv.getLineCount();//5 lines tv.getText();
Note: When TextView has no parent control, it cannot call the textView.setText() method multiple times (2 or more times), otherwise it will cause a crash. This is because it does not have layoutParams. The solution is to set layoutParams first, and call the onMeasure() method directly. The measurement does not affect its actual size. The actual size that affects it is the layoutParams property.
Result: create a TextView and set its width to match_parent, with a maximum screen width of 1080px and a height of wrap_content, set the text to 100 "H", the measured width is 1080px, the height is 137px, and three lines of text; Again, set the text to 200 "H" s, measure the width and height to 1080*223, and set the number of lines to 5. the actual width and height measured after adding the view to the layout is consistent with the measured width and height
Note: If textView sets properties such as maxLines padding, the measured size will also take effect.
Application Scenario 2: The following list, the data of the button is determined by the background return, the text of each button is long or short, there is a minimum spacing between each button, and the length and width are equal. After acquiring the data, it is necessary to measure the width of the screen to accommodate several buttons in one line, and then determine the display of the list according to the measurement results.
Application Scenario 3: Calculate the height of a slightly complex item layout and the size of a view in the layout.
In the following code, the textView control in an item layout uses parameters such as weight, padding, margin, maxLines, etc. It is necessary to measure the height of the item and the size of the textView after setting the text.
Solution: Measure the entire item layout, get the textView object to get its measured size.
//Get the View object of the layout View rootView = LayoutInflater.from(this).inflate(R.layout.item_nice, null, false); //Get the textView in the layout TextView textView = rootView.findViewById(R.id.expandable_text); textView.setText(getNewStr("H",100));//Set 100 H int screenWidth = ScreenUtils.getScreenWidth(); int width = screenWidth / 1; int wrapContent = ViewGroup.LayoutParams.WRAP_CONTENT; int w = View.MeasureSpec.makeMeasureSpec(ScreenUtils.getScreenWidth(), View.MeasureSpec.EXACTLY); int h = View.MeasureSpec.makeMeasureSpec(wrapContent, View.MeasureSpec.AT_MOST); rootView.measure(w,h);//Measure root layout int measuredHeight1 = textView.getMeasuredHeight();//textView get measure width and height int measuredWidth1 = textView.getMeasuredWidth();