Android can obtain width and height information in the onCreate () method.
I encountered a small Demo for my friend's project:
This listener also knows the name, that is, it is called before the painting is completed. The number of rows can be obtained here, and of course the width and height information can also be obtained.
Package com. example. textviewtest; import android. annotation. suppressLint; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. view. viewTreeObserver; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private TextView text; private Button button ;@ SuppressLint ("NewApi") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); text = (TextView) findViewById (R. id. text2); button = (Button) findViewById (R. id. button); text. setText ("Guangxi News Network-South China News Liuzhou news a consumer to the hair salon, due to the effect of the hair, the claim for thousands to 10 thousand Yuan compensation, and through industrial and commercial mediation fails, then, the poster harassed the shopkeeper and alerted the police. The police intervened patiently to do their work. After the consumer received the compensation proposal from the shopkeeper after the police and industry and commerce witnessed it, the two parties turned the solution into "); // ViewTreeObserver vto = text is notified when the global event of the view tree is changed. getViewTreeObserver (); // The Listener obtains the vto function. addOnPreDrawListener (new ViewTreeObserver. onPreDrawListener () {@ Overridepublic boolean onPreDraw () {// get the height of the text View int lineCount = text. getLineCount (); System. out. println (lineCount); // logic judgment. If the value is greater than 2, the button is displayed. If the number of lines is less than or equal to 2, the button is hidden. If (lineCount> 2) {button. setVisibility (View. VISIBLE);} else {button. setVisibility (View. GONE) ;}return true ;}}); button. setOnClickListener (new OnClickListener () {Boolean flag = true; @ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubLog. I ("zkk", text. getHeight () + ""); if (flag) {flag = false; text. setEllipsize (null); // expand text. setSingleLine (flag); button. setText ("hidden");} else {flag = true; text. setMaxLines (2); // Contract the button. setText ("show"); // text. setEllipsize (TruncateAt. END );}}});}}