I. Many children's shoes may not understand the usage of getWidth () and getMeasuredWidth (). What are the differences between the two, there are different versions on the Internet, but most of them share the same small version, from this place Ctrl + C, to another place Ctrl + V, I have not thoroughly explained this question. I have also been deeply affected by some articles that have written my understanding of these two methods. In this case, I want to explain a lot of statements in the following version. Baidu has searched a lot. Unfortunately, this statement is not correct, so I hope that you will not blindly try again in your space:
GetWidth indicates the actual size of a view.
GetMeasuredWidth indicates the size of a view in the parent view.
Surely you have seen such a solution. It seems that such a solution is also within the classroom, and you have not understood the problem.
II. Okay, the zookeeper version is not mentioned much. Let's make a positive solution to these two methods. First, we should know the following points:
1. When a class is initialized, we cannot get the actual size of the View in the structure function. If you are interested, you can refer to getWidth () and getMeasuredWidth (). The result is 0, but we can get the control size from the onDraw () method.
2. The result of the two methods is pixel.
Introduce the two methods:
GetWidth ():The result shows the degree of view after the parent Layout is properly configured in the parent Layout. If there is no parent board, then the entire screen is recognized as the parent board. It may be hard to understand. Let's illustrate through an example.
Example 1:
Public class Test extends Activity {
Private LinearLayout mBackgroundLayout;
Private TextViewTest mTextViewTest;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MBackgroundLayout = new MyLayout (this );
MBackgroundLayout. setLayoutParams (new LinearLayout. LayoutParams (
LinearLayout. LayoutParams. FILL_PARENT,
LinearLayout. LayoutParams. FILL_PARENT ));
MTextViewTest = new TextViewTest (this );
MBackgroundLayout. addView (mTextViewTest );
SetContentView (mBackgroundLayout );
}
Public class MyLayout extends LinearLayout {
Public MyLayout (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}
@ Override
Protected void onLayout (boolean changed, int l, int t, int r, int B ){
// TODO Auto-generated method stub
Super. onLayout (changed, l, t, r, B );
Log. I ("Tag ","--------------");
View mView = getChildAt (0 );
MView. measure (0, 0 );
}
}
Public class TextViewTest extends TextView {
Public TextViewTest (Context context ){
Super (context );
// TODO Auto-generated constructor stub
SetText ("test ");
}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
// Measure (0, 0 );
Log. I ("Tag", "width:" + getWidth () + ", height:" + getHeight ());
Log. I ("Tag", "MeasuredWidth:" + getMeasuredWidth ()
+ ", MeasuredHeight:" + getMeasuredHeight ());
}
}
}
Add a TextView control in LinearLayout. If you want to get getWidth () for the TextView cursor at this time, then add the TextView to Layout and set the value again, it is not a matter of order to take the margin of the TextView itself.
GetMeasuredWidth ():Let's take a look at what the API says.
The
Width of this view as measured in the most recent call to measure ().
This shoshould be used during measurement and layout calculations only.
The result is the view degree obtained after the latest increment using the measure () method, which is used in the calculation of the increment volume and layout.
Therefore, this method is used to obtain the actual attention of the view content manager.
If you want to change from the simplest example to their differences, we will modify the above example as follows:
Public class Test extends Activity {
Private TextViewTest mTextViewTest;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MTextViewTest = new TextViewTest (this );
SetContentView (mTextViewTest );
}
Public class TextViewTest extends TextView {
Public TextViewTest (Context context ){
Super (context );
// TODO Auto-generated constructor stub
SetText ("test ");
}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
Measure (0, 0 );
Log. I ("Tag", "width:" + getWidth () + ", height:" + getHeight ());
Log. I ("Tag", "MeasuredWidth:" + getMeasuredWidth ()
+ ", MeasuredHeight:" + getMeasuredHeight ());
}
}
}
Summary (positive solution ):
GetWidth (): Specifies the overall View degree after setting the resolution.
GetMeasuredWidth ():
The consistency of the View content after the volume of content on the View is calculated, provided that you have to onLayout () method or in the onDraw () method of this View
Use measure (); (you can define the value of measure values). Otherwise, the result is the same as that obtained by getWidth.
Maybe my organization is not very good. If you have any questions, leave a message for me. The difference between the two methods is that you have not used the measure () method. Of course, the location of measure () is also very important.
3. Respect the original website. Please refer to the empty website at http://hi.baidu.com/ljlkings/home.