There is a bug in the previous article, I will come again
1. Create a layout collapsible_textview_layout.xml
The layout can be modified by its own style
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dip"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="left" android:gravity="left" android:orientation="horizontal"> <TextView android:id="@+id/desc_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:lineSpacingExtra="3dp" android:text="asdasd" android:textColor="#4A4A4A" android:textSize="12sp" /> </LinearLayout> <LinearLayout android:id="@+id/desc_op_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" > <TextView android:id="@+id/desc_op_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="expand" /> </LinearLayout> </LinearLayout>
2. After the layout is created, start writing custom controls. First get the required layout in the constructor.
public class CollapsibleTextView extends LinearLayout implements View.OnClickListener{ //Specify the maximum number of lines of text private int TEXT_MAX_LINE = 3; // Is it collapsible private boolean IS_COLLAPSIBLE; // Is it already folded private boolean COLLAPSIBLE; / / The first start to enter the onLayout method mark private boolean flag; // drop down arrow layout private LinearLayout llDescOp; // drop down arrow private TextView ivDescOp; //text content private TextView tvDesc; //Listen to the click event of the drop-down arrow, and respond accordingly by judging the state of the text box class InnerRunnable implements Runnable{ @Override public void run() { if(IS_COLLAPSIBLE && COLLAPSIBLE){ // Collapsible and already collapsed -- text expands tvDesc.setMaxLines(Integer.MAX_VALUE); // ivDescOp.setImageResource(R.mipmap.ic_launcher_round); ivDescOp.setText("Retract"); COLLAPSIBLE = false; flag = true; }else if(IS_COLLAPSIBLE && !COLLAPSIBLE){ // Collapsible but not collapsing -- text collapsing tvDesc.setMaxLines(TEXT_MAX_LINE); // ivDescOp.setImageResource(R.mipmap.ic_launcher); ivDescOp.setText("Expand"); COLLAPSIBLE = true; flag = true; } } } public CollapsibleTextView(Context context, AttributeSet attrs) { super(context, attrs); //Get the layout of the view View view = inflate(context, R.layout.collapsible_textview_layout,this); tvDesc = (TextView) view.findViewById(R.id.desc_tv); llDescOp = (LinearLayout) view.findViewById(R.id.desc_op_ll); ivDescOp = (TextView) findViewById(R.id.desc_op_iv); llDescOp.setOnClickListener(this); } public CollapsibleTextView(Context context) { super(context); } //Set the content method of the text for the caller to use. public void setText(String text){ tvDesc.setText(text); requestLayout(); } //The obtained layout is added to the control @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); //initialization code //flag is used to mark subsequent operations without the following code if(!flag){ //The number of lines of text is greater than the specified value, collapse and display the drop-down arrow if(tvDesc.getLineCount() > TEXT_MAX_LINE){ tvDesc.setMaxLines(TEXT_MAX_LINE); llDescOp.setVisibility(VISIBLE); IS_COLLAPSIBLE = true; COLLAPSIBLE = true; }else{ llDescOp.setVisibility(GONE); IS_COLLAPSIBLE = false; } } } @Override public void onClick(View view) { post(new InnerRunnable()); } }
3. Reference this control in our layout activity_main. Pay attention to the format and write the path of the full control.
<com.guorentong.learn.textviewzhedie.CollapsibleTextView android:id="@+id/CollapsibleTextView" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.guorentong.learn.textviewzhedie.CollapsibleTextView>
4. Code call settings
collapsibleTextView = (CollapsibleTextView) findViewById(R.id.CollapsibleTextView); //Set the text to appear collapsibleTextView.setText("文本");