Transferred from: http://blog.csdn.net/lizzy115/article/details/7513552
Float
Other than that:
1) Questions
When you implement an adaptive Tablectrl control in Android to display text information, there is a problem when you need to get the width value of the longest string in a column. After searching the internet, I discovered that Android can take advantage of the Measuretext (Stringtext) method of the paint already provided in the SDK. So try the code that has been given online:
Paint paint = new paint (); float strwidth = Paint.measuretext (String);
The columns in the control are called by the above and get the maximum width, truncated when the string display is set, and the value returned is still too small. Is it related to mixed fonts? Is it possible that the resulting paint instance only uses the default value?
(2) Process of settlement
When I thought of paint as an important element in drawing, I noticed that the familiar TextView was the way Getpaint () returned the instance of Textpaint (the base paint used for thetext. Consult the Paint ' s properties and not to change them.) The use of paint is associated with resources in a particular context and can now be solved. Use the context that has been saved in Tablectrl (actually an instance of activity) and make the following call:
View Sourceprint? TextView TextView = new TextView (mcontext);
Paint paint = TextView. Getpaint ();
View Sourceprint?float textWidth = Paint.measuretext (text);
After testing, the TextWidth value you get at this point is what you really want. Thus the Tablectrl cell can be reached by the longest text information automatically expanded display.
(3) Further discussion
Now we can turn our attention to TextView, which has an important parameter context in its constructor, to see the explanation of the parameter context in the constructor of its parent view: Context, the context the view is running in, Through which it can access the current theme, resources, etc. It is easy to see that the view display is dependent on the external context.
View Sourceprint?public TextView (context context, AttributeSet attrs, int defstyle)
{
Super (context, attrs, Defstyle);
...
Mtextpaint = new Textpaint (Paint.anti_alias_flag);
Mtextpaint.density = GetResource (). Getdisplaymetrics (). density;
...
}
By the above TextView implementation of the relevant part of the source code, you can find that Textpaint and the context available resources are actually closely related, it is easy to know what happened to the Measuretext method before the call.
It is often seen in the OnCreate () method of the activity-derived class that we typically call the Setcontentview () setting to display the view associated with the current activity, The following is also a general look at the process: the activity's Setcontentview () method is actually left to its members---the Window class instance Mwindow (Notice that window is an abstract class, The actual Mwindow is an example of a phonewindow produced by a factory method). Continue digging Phonewindow rewrite the relevant source code for the Setcontentview () method:
View[email protected]
public void Setcontentview (view view, Viewgroup.layoutparams params) {
if (mcontentparent = = null) {
Installdecor ();
} else {
Mcontentparent.removeallviews ();
}
Mcontentparent.addview (view, params);
Final Callback cb = Getcallback ();
if (CB! = null) {
Cb.oncontentchanged ();
}
}
Mwindow is added as a top-level view in activity, and you can see that after you set up the view, Phonewindow gets the external callback method for the initial registration, which triggers the call to implement the corresponding update operation.
Android get TextView text actual width