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