Custom controls that can be scaled TextView

Source: Internet
Author: User

Project requirements, some textview if more than two lines, need to hide, there is a picture on the right, click can open to see all the content, you can also click contraction.;ImportAndroid.content.Context;;Importandroid.text.Editable;ImportAndroid.text.TextUtils;ImportAndroid.text.TextWatcher;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportZIVIXGROUP.COM.SKILLTEMP.R;/** * can stretch TextView * Created by George.yang on 2015/8/7. */ Public  class stretchtextview extends TextView implements View. Onclicklistener {    Private intMaxLines =2;Private BooleanIsspread;//Stretch status    PrivateOnclicklistener Listener;Private intSpreadheight;//height at unfold     Public Stretchtextview(Context context) {Super(context);    Init (context); } Public Stretchtextview(context context, AttributeSet attrs) {Super(context, attrs);    Init (context); } Public Stretchtextview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Init (context); }Private void Init(Context context) {setellipsize (TextUtils.TruncateAt.END);//superfluous to become ...Setmaxlines (MaxLines); Addtextchangedlistener (NewTextwatcher () {@Override             Public void beforetextchanged(Charsequence charsequence,intIintI1,intI2) {}@Override             Public void ontextchanged(Charsequence charsequence,intIintI1,intI2) {}@Override             Public void aftertextchanged(Editable Editable) {postdelayed (NewRunnable () {@Override                     Public void Run() {if(Getlinecount () >= maxLines) {Setcompounddrawableswithintrinsicbounds (0,0, R.drawable.ic_details_more,0); }Else{Setcompounddrawableswithintrinsicbounds (0,0,0,0); }                    }                }, -); }        });Super. Setonclicklistener ( This); }@Override     Public void Setonclicklistener(Onclicklistener L)    {listener = l; }@Override     Public void OnClick(View view) {if(Getlinecount () <maxlines) {return; } isspread =!isspread;if(Isspread) { This. Setmaxlines (Integer.max_value); This. Requestlayout ();if(spreadheight==0) {Setcompounddrawableswithintrinsicbounds (0,0, R.drawable.ic_details_more,0); Postdelayed (NewRunnable () {@Override                     Public void Run() {spreadheight = Getmeasuredheight ();                    Resetdrawable (); }                }, -); }Else{resetdrawable (); }        }Else{ This. Setmaxlines (MaxLines); This. Requestlayout ();            Spreadheight = Getmeasuredheight (); Setcompounddrawableswithintrinsicbounds (0,0, R.drawable.ic_details_more,0); }if(listener!=NULL) {Listener.onclick (view); }    }Private void resetdrawable() {drawable drawable = getresources (). getdrawable (R.DRAWABLE.IC_SHRINK_UP); Drawable.setbounds (0, spreadheight/2-drawable.getintrinsicheight ()/2, Drawable.getintrinsicwidth (), spreadheight/2+drawable.getintrinsicheight ()/2); Setcompounddrawables (NULL,NULL, Drawable,NULL); }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Custom controls that can be scaled TextView

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: 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.