Android Gets the height of the TextView control dynamically in the OnCreate () method

Source: Internet
Author: User

just a friend of the project encountered to write a small demo:

The listener knows the name as well. It is called before the painting is finished, in which the number of rows can be obtained. Of course, we can get the information of wide and high



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-southern This newspaper Liuzhou News a consumer to the hair salon, because of the hairdressing effect dissatisfaction. Ask for compensation of thousands of to 10,000 yuan, and the business mediation fails, and then big "+" make the salon harassment shopkeeper, and alarmed the police. Police involved in patient work, consumers after the police and business witness, accept the owner's compensation scheme, the two sides hatchet ");//Get notified when global events change for a view tree viewtreeobserver vto = Text.getviewtreeobserver ( );//listen to get back function Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {@Overridepublic Boolean onpredraw () {//Gets the height of the text View int linecount = text. Getlinecount (); System.out.println (LineCount);//logic inference. If the display button is greater than 2, it is hidden if the number of rows is less than or equal to 2.

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 Aut O-generated method Stublog.i ("Zkk", text.getheight () + ""); if (flag) {flag = False;text.setellipsize (null);// Expand Text.setsingleline (flag); Button.settext ("Hide");} else {flag = True;text.setmaxlines (2);//Shrink Button.settext ("show");//text.setellipsize (truncateat.end);}});}}


Here is the layout interface


<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/text1 "android:layout_width=" Wrap_content "Androi d:layout_height= "Wrap_content" android:text= "@string/hello_world"/> <relativelayout android:layout _width= "Match_parent" android:layout_height= "wrap_content" android:layout_below= "@id/text1" Android: layout_margintop= "10DP" > <textview android:id= "@+id/text2" android:layout_width= "Match_ Parent "android:layout_height=" Wrap_conteNT "android:maxlines=" 2 "/> <button android:id=" @+id/button " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignpa Rentright= "true" android:layout_margintop= " -10DP" android:text= "drop-down"/> </RelativeLayout> </RelativeLayout>


Android Gets the height of the TextView control dynamically in the OnCreate () method

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.