Android 之TextView文本折叠查看显示更多效果
来源:互联网 发布:软件著作权申报 编辑:程序博客网 时间:2024/05/22 23:04
- 自定义CollapsibleTextView继承LinearLayout
/** * 易皇星 * Created by Administrator on 2016/3/22. */public class CollapsibleTextView extends LinearLayout implements OnClickListener { /** default text show max lines */ private static final int DEFAULT_MAX_LINE_COUNT = 2; private static final int COLLAPSIBLE_STATE_NONE = 0; private static final int COLLAPSIBLE_STATE_SHRINKUP = 1; private static final int COLLAPSIBLE_STATE_SPREAD = 2; private TextView desc; private TextView descOp; private String shrinkup; private String spread; private int mState; private boolean flag; public CollapsibleTextView(Context context, AttributeSet attrs) { super(context, attrs); // 两种定义形式// shrinkup = context.getString(R.string.desc_shrinkup);// spread = context.getString(R.string.desc_spread); shrinkup="收起"; spread="查看更多"; View view = inflate(context, R.layout.collapsible_textview, this); view.setPadding(0, -1, 0, 0); desc = (TextView) view.findViewById(R.id.desc_tv); descOp = (TextView) view.findViewById(R.id.desc_op_tv); descOp.setOnClickListener(this); } public CollapsibleTextView(Context context) { this(context, null); }/** 1. 2. 对外提供暴漏的方法,为文本提供数据 3. @param charSequence 文本内容是什么 4. @param bufferType */ public final void setDesc(CharSequence charSequence, BufferType bufferType) { desc.setText(charSequence, bufferType); mState = COLLAPSIBLE_STATE_SPREAD; requestLayout(); } @Override public void onClick(View v) { flag = false; requestLayout(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (!flag) { flag = true; if (desc.getLineCount() <= DEFAULT_MAX_LINE_COUNT) { mState = COLLAPSIBLE_STATE_NONE; descOp.setVisibility(View.GONE); desc.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1); } else { post(new InnerRunnable()); } } } class InnerRunnable implements Runnable { @Override public void run() { if (mState == COLLAPSIBLE_STATE_SPREAD) { desc.setMaxLines(DEFAULT_MAX_LINE_COUNT); descOp.setVisibility(View.VISIBLE); descOp.setText(spread); mState = COLLAPSIBLE_STATE_SHRINKUP; } else if (mState == COLLAPSIBLE_STATE_SHRINKUP) { desc.setMaxLines(Integer.MAX_VALUE); descOp.setVisibility(View.VISIBLE); descOp.setText(shrinkup); mState = COLLAPSIBLE_STATE_SPREAD; } } }}
- collapsible_textview.xml定义
<?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" > <TextView android:id="@+id/desc_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4.0dip" android:gravity="center_vertical" android:textColor="#ff000000" android:textSize="14.0dip" /><!--可以灵活的将收藏的按钮至右 至左 居中 --> <TextView android:id="@+id/desc_op_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4.0dip" android:gravity="right" android:singleLine="true" android:textColor="#ff576b95" android:textSize="14.0dip" android:visibility="gone" /></LinearLayout>
3.使用:
public class Main extends Activity { String s = "但发斯蒂芬的佛教啊哦啊二等奖发了恐惧似懂非懂,爱递交奇怪阿尔发票奥支付到上世纪的阿斯顿发送到撒旦法大的事发的爱的色放的阿斯顿发阿斯顿发阿萨德发的多发点"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); CollapsibleTextView tv = (CollapsibleTextView) findViewById(R.id.desc_collapse_tv); tv.setDesc(s, BufferType.NORMAL); }}
效果图:
4 0
- Android 之TextView文本折叠查看显示更多效果
- Android TextView 文本折叠效果
- Android TextView 文本折叠效果
- Android TextView 文本折叠效果
- Android TextView多文本折叠展开效果
- Android TextView查看更多
- TextView的查看更多效果
- TextView多文本折叠/展开效果
- Android 查看更多 TextView实现
- Android文本显示:TextView
- Android尾部带“查看更多”的TextView
- Android尾部带“查看更多”的TextView
- Android尾部带“查看更多”的TextView
- Android控件之文本控件---TextView 两种效果+SpannableString
- android textview 显示 文本 .txt
- Android显示TextView文本控件
- android textview 文本显示不同颜色
- TextView上超炫的文本显示效果
- iOS应用获取当前设备的bundle_id
- JPA-style positional param was not an integral ordinal
- caffe中卷积计算方法
- Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]
- 剑指offer 1 二维数组中的查找
- Android 之TextView文本折叠查看显示更多效果
- 工厂方法
- 深入理解HTTP协议(转)http协议学习系列
- android之adapter的抽象与提炼
- react-lifecycle-parent-child.jsx(自github)
- android:layout_gravity="bottom"无效
- 使用CSS实现一个flash动画
- hihoCoder 1038 01背包
- PAM(二)