Android实现可以展开的TextView
程序员文章站
2022-06-04 10:45:33
本文实例为大家分享了一个可以展开的textview的具体代码,供大家参考,具体内容如下
原理是通过点击的时候设置 setmaxlines(linenum)来实现:...
本文实例为大家分享了一个可以展开的textview的具体代码,供大家参考,具体内容如下
原理是通过点击的时候设置 setmaxlines(linenum)来实现:
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; /** 点击事件listener */ private onclicklistener monclicklistener; public expandabletextview(context context, attributeset attrs) { super(context, attrs); init(context, attrs); } private void init(context 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 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); } }
这样来写layout:
<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="adfsfsfewrwerwerwerqrwgdffdfdfersafsdfsdfsdd" 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>
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 手动注入bean