自定义TextView显示指定行数
来源:互联网 发布:mysql text 最大长度 编辑:程序博客网 时间:2024/06/05 15:47
接到需求,大段的商品介绍,默认情况先显示3行,其他没显示完的内容折叠,点击箭头后,展开显示全部文本。
其实很简单,只需要通过行高*行数,动态的设置TextView的高度。
直接贴代码 :
/** * 多行textView显示指定行数 * Date: 2016-08-25 * * @author honjane */public class LineTextView extends LinearLayout { private TextView mContentView; private ImageView mArrowView; private int mMaxLine; private String mTextContent; public LineTextView(Context context) { this(context, null); } public LineTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public LineTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); initAttr(attrs); bindListener(); } private void initView() { setOrientation(VERTICAL); mContentView = new TextView(getContext()); addView(mContentView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); mArrowView = new ImageView(getContext()); mArrowView.setImageResource(R.drawable.arrow); int padding = dip2px(getContext(), 5); mArrowView.setPadding(padding, padding, padding, padding); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER_HORIZONTAL; addView(mArrowView, params); } private void initAttr(AttributeSet attrs) { TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.LineTextStyle); int textColor = typeArray.getColor(R.styleable.LineTextStyle_textColor, Color.BLACK); int textSize = typeArray.getDimensionPixelSize(R.styleable.LineTextStyle_textSize, 12); mMaxLine = typeArray.getInt(R.styleable.LineTextStyle_line, 2); mTextContent = typeArray.getString(R.styleable.LineTextStyle_text); bindTextView(textColor, textSize, mMaxLine, mTextContent); typeArray.recycle(); } private void bindTextView(int textColor, int textSize, final int maxLine, String textContent) { mContentView.setText(textContent); mContentView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); mContentView.setTextColor(textColor); mContentView.setHeight(mContentView.getLineHeight() * maxLine); //保证绘制完成才能回去到getLineCount值 post(new Runnable() { @Override public void run() { mArrowView.setVisibility(mContentView.getLineCount() > maxLine ? VISIBLE : GONE); } }); } private void bindListener() { setOnClickListener(new OnClickListener() { boolean isExpand; @Override public void onClick(View v) { isExpand = !isExpand; mContentView.clearAnimation(); final int deltaValue; final int startValue = mContentView.getHeight(); int duration = 300; //arrow 倒转动画 if (isExpand) { deltaValue = mContentView.getLineHeight() * mContentView.getLineCount() - startValue; RotateAnimation expandAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); expandAnimation.setDuration(duration); expandAnimation.setFillAfter(true); mArrowView.startAnimation(expandAnimation); } else { deltaValue = mContentView.getLineHeight() * mMaxLine - startValue; RotateAnimation foldAnimation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); foldAnimation.setDuration(duration); foldAnimation.setFillAfter(true); mArrowView.startAnimation(foldAnimation); } //text 展开动画 Animation textAnim = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation trans) { super.applyTransformation(interpolatedTime, trans); mContentView.setHeight((int) (startValue + deltaValue * interpolatedTime)); } }; textAnim.setDuration(duration); mContentView.startAnimation(textAnim); } }); } public String getText() { return mTextContent; } public void setText(CharSequence charSequence) { mContentView.setText(charSequence); } public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); }}
attr.xml文件
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="LineTextStyle"> <attr name="textSize" format="dimension"/> <attr name="textColor" format="color"/> <attr name="line" format="integer" /> <attr name="text" format="string" /> </declare-styleable></resources>
使用还是与TextView用法相似,没什么改动,使用的时候注意命名空间
<com.honjane.linetext.LineTextView xmlns:more="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dip" more:textColor="@android:color/black" more:textSize="14dip" more:line="3" more:text="@string/text"/>
0 0
- 自定义TextView显示指定行数
- Textview显示指定行数
- TextView显示行数问题
- Android中如何让TextView显示指定的行数并且多出的部分显示省略号
- TextView显示指定的行数并且多出的部分显示省略号
- TextView内容过长显示指定行数加省略号,点击显示全部内容
- Android中让TextView显示指定的行数并且多出的部分显示省略号
- 动态设置textview显示行数
- 让TextView显示指定的行数,超过部分用省略号代替
- TextView显示指定的行数并且多出的部分显省略号
- Androi实战—EidtText和TextView显示指定行数,内容过长自动换行问题
- TextView的字数行数限制显示
- [安卓自定义控件]自定义组合控件——支持根据TextView行数展开折叠的引文显示控件ExpandableReferenceView
- Textview指定长度显示省略号
- React Native 指定文字行数,超出行数显示更多
- 显示文件的头尾指定行数
- 读取指定行数文件内容并显示
- android textview字儿数多于指定行数用省略号代替
- Java Web学习——问题罗列
- 回文串划分
- 高并发常见的解决方案
- 关于linux中使用vim打开文件出现^M的解决方法
- poj 2777 Count Color 线段树区间更新
- 自定义TextView显示指定行数
- MAC ping IP 地址(ping通/ping不通)
- Tomcat7配置数据源
- 自定义TextView处理任意时间长按短按的事件
- java G1垃圾收集器
- 关于使用公钥进行签名验证的代码
- ReactNative 学习(坑-Applocation xxxx has not been registered)
- workbench 连接mysql数据库失败
- 拉青电站调速器发货