Introduced
In the application development, always encounter some similar to the announcement, explain the textview of the long text, but in order to typography and other factors, we usually want to hide the second half of the text, only show some text, and then at the end of the user will be provided with an extension/contraction of the button, So that the text box can be expanded when needed to view the full text, which requires implementing a expendabletextview, similar to expendablelist.
Principle
1, start using Android:lines to set the number of TextView, click the button, remove the limit.
2. Use Android:ellipsize to set the omitted position of the text.
3, remember to set android:layout_height= "Wrap_content", or fixed a height, it can not be achieved.
Realize
Main.xml layout
<Relativelayoutxmlns: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:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.appupdate.MainActivity" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:ellipsize= "End"Android:lines= "3"Android:text= "I was stating that I was an announcement I was stating I was an announcement I was stating I was an announcement I was stating I was an announcement I was stating I was an announcement I was stating I was the announcement I was stating I was the announcement I was yes, I'm an announcement, I'm an announcement, I'm a statement, I'm an announcement, I'm an announcement, I'm a statement, I'm an announcement, I'm a statement, I'm an announcement, I'm a statement, I'm an announcement, I'm saying Ming I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement I was stating I was the announcement "Android:textcolor= "@android: Color/black" /> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/textview1"Android:layout_centerhorizontal= "true"Android:text= "Click to expand" /></Relativelayout>
Core code
PrivateTextView TextView1; PrivateButton button1; Private BooleanIsexpend =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView1=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Button1=(Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(!isexpend) { //RecoveryTextview1.setminlines (0); Textview1.setmaxlines (Integer.max_value); Button1.settext ("Click to shrink"); Isexpend=true; } Else{textview1.setlines (3); Button1.settext ("Click to expand"); Isexpend=false; } } }); }
It looks like this after it's run.
Find a time to refactor, make a custom control, achieve a true sense of expendabletextview
Android Implementation Expendabletextview Extensible TextView