Andriod dynamically sets TextView and RelativeLayou height, andriodtextview

Source: Internet
Author: User

Andriod dynamically sets TextView and RelativeLayou height, andriodtextview

1 XML layout 2 <RelativeLayout 3 android: id = "@ + id/rlay_meeting_contact_context" 4 android: layout_width = "match_parent" 5 android: layout_height = "44dp" 6 android: clickable = "false" 7 android: focusable = "false"> 8 9 <TextView10 android: id = "@ + id/TV _test" 11> 12 android: layout_width = "match_parent" 13 android: layout_height = "match_parent" 14 android: layout_alignParentTop = "true" 15 android: layout_toRightOf = "@ + id/TextView07" 16 android: duplicateParentState = "true" 17 android: gravity = "left | center" 18 android: singleLine = "true" 19 android: text = "@ string/accept_invite_this_troop"/> 20 21 </RelativeLayout>

Note: android: gravity = "left | center" // indicates that the text starts from the middle of the left. Otherwise, the text is displayed in the center after being expanded.

 

JAVA

Definition:

Private int mContextHeght = 0;
Private Boolean mLookContextflag = false;

private RelativeLayout mrlayMeetingContactContext;mrlayMeetingContactContext = (RelativeLayout) findViewById(R.id.rlay_meeting_contact_context);

 

1 private TextView  mtvTest;2 3 mtvTest= (TextView) findViewById(R.id.tv_test);

 

Obtain the LineCount using the timer.

Note: getLineCount must be expanded before it can be obtained correctly. Otherwise, it is 0.

 

1 TimerTask task = new TimerTask () {2 public void run () {3 Message message = new Message (); 4 message. what = 1; 5 handler. sendMessage (message); 6} 7}; 8 9 final Handler handler = new Handler () {10 public void handleMessage (Message msg) {11 switch (msg. what) {12 case :13 android. util. log. e ("Timer", "Timer"); 14 update (); 15 break; 16} 17 super. handleMessage (msg); 18} 19 20}; 21 22 private void upda Te () {23 int linecount = mtvTest. getLineCount (); 24 25 Log. I ("TV. getLineCount () ", mtvTest. getHeight () + ""); 26 if ((! MLookContextflag) | (linecount <2) {27 28 mLookContextflag = true; 29 mtvTest. setEllipsize (null); // expand 30 mtvTest. setSingleLine (false); 31} else if (mLookContextflag) | (linecount> 1) {32 mLookContextflag = false; 33 if (mtvTestt. getLineCount ()> 1) {34 int iHeght = mtvTest. getLineCount () 35 * mtvTest. getLineHeight (); 36 37 if (iHeght> mContextHeght) {38 android. view. viewGroup. layoutParams pp = MrlayMeetingContactContext39. getLayoutParams (); 40 pp. height = iHeght; 41 mrlayMeetingContactContext. setLayoutParams (pp); 42 mMeetingContactContext. setHeight (iHeght); 43 // when timer is used up, it stops 44 if (mTimer! = Null) {45 mTimer. cancel (); 46 mTimer = null; 47} 48} 49 50} 51 52}

 

Call:

 1     @Override 2     public void onClick(View v) { 3         // TODO Auto-generated method stub 4         switch (v.getId()) { 5         case R.id.meeting_contact_context: 6             if (mContextHeght == 0) { 7                 mContextHeght = mrlayMeetingContactContext.getHeight(); 8                 if (mTimer == null) { 9                     mTimer = new Timer(false);10                     mTimer.schedule(task, 10, 100);11                 }12             }13 }

 

Related Article

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.