Android can obtain width and height information in the onCreate () method.

Source: Internet
Author: User

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 );}}});}}


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.