How to Write a textview that can be expanded

Source: Internet
Author: User

The principle is to set it when you clickSetmaxlines (linenum)To achieve:

Public class expandabletextview extends textview {Private Static int min_line_num = 2; Private Static int max_line_num = 20; private int linenum = min_line_num; private Boolean misexpanded = false; bitmap bitmapdown; bitmap bitmapup; int W, H;/** Click Event listener */private onclicklistener monclicklistener; Public expandabletextview (context, attributeset attrs) {super (context, attrs); Init (C Ontext, attrs);} private void Init (context, attributeset attrs) {setmaxlines (linenum); bitmapdown = bitmapfactory. decoderesource (getresources (), R. drawable. update_detail_down); bitmapup = bitmapfactory. decoderesource (getresources (), R. drawable. update_detail_up); W = bitmapdown. getwidth () + (INT) context. getresources (). getdimension (R. dimen. expand_textview_right); H = bitmapdown. getheight () + (INT) context. getresources (). getdimension (R. dimen. expand_textview_bottom); super. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {If (linenum = min_line_num) {linenum = max_line_num; setmaxlines (linenum );} else {linenum = min_line_num; setmaxlines (linenum);} If (monclicklistener! = NULL) {monclicklistener. onclick (v) ;}}) ;}@ override public void setonclicklistener (onclicklistener listener) {If (! Isclickable () {setclickable (true);} monclicklistener = listener;} @ override public void setmaxlines (INT maxlines) {super. setmaxlines (maxlines); If (linenum = max_line_num) {misexpanded = true;} else {misexpanded = false;} public Boolean isexpanded () {return misexpanded ;} @ override protected void ondraw (canvas) {If (getlinecount () <= min_line_num) {super. ondraw (canvas); return;} If (linenum = min_line_num) {canvas. drawbitmap (bitmapdown, getwidth ()-W, getheight ()-H, null);} else {canvas. drawbitmap (bitmapup, getwidth ()-W, getheight ()-H, null);} super. ondraw (canvas);} public void setmaxline (INT line) {linenum = line; setmaxlines (line );}}

Write layout as follows:

<RelativeLayout xmlns: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:background="#FFFFFF" >    <TextView        android:id="@+id/detail_action"        android:textColor="#000000"        android:textSize="11sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginRight="8dip"        android:drawablePadding="4dip"        android:paddingBottom="10dip"        android:paddingTop="10dip"        android:singleLine="true"        android:text="sdfkljsfiosjdfiosfjsiofjsdifsd" />    <com.example.view.ExpandableTextView        android:id="@+id/textview"        android:layout_width="100dp"        android:layout_height="wrap_content"        android:layout_below="@id/detail_action"        android:lineSpacingExtra="4dip"        android:paddingBottom="-40dip"        android:paddingRight="48dip"        android:text="adfsfsfewrwerwerwerqRWgdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfERSAFSDFSDFSDd"        android:layout_marginTop="-5dip"        android:textColor="#000000" >    </com.example.view.ExpandableTextView>             <TextView        android:id="@+id/detail_action1"        android:textColor="#000000"        android:textSize="11sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/textview"        android:layout_marginRight="8dip"        android:drawablePadding="4dip"        android:paddingBottom="10dip"        android:paddingTop="10dip"        android:singleLine="true"        android:text="sdfkljsfiosjdfiosfjsiofjsdifsd" /></RelativeLayout>


How to Write a textview that can be expanded

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.