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.
PackageZivixgroup.com.skilltemp.widget;ImportAndroid.content.Context;Importandroid.graphics.drawable.Drawable;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); }}
Ic_shrink_up
Ic_details_more
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Custom controls that can be scaled TextView