我们在移动开发过程中经常会遇到文本的展示问题,因为手机屏幕空间是有限的,所以经常会出现文本 显示不下的问题,我在开发的时候就遇到过产品让我展示一段文本在一行上,如果多于一行则显示..., 点击...可以展开看所有文字,这种需求在展示通讯录的名字上比较常用
思路是在用text填充控件,然后使用来获取文本的宽度,把这个宽度跟屏幕宽度来对比,看
Paint paint = this.getPaint(); mTextWidth = paint.measureText(mText);
是否超出屏幕宽度,超出了就要计算文本要显示多少
if (mTextWidth > maxWidth && !mIsClicked) { mLinesable = true; float measuredWidth[] = new float[mText.length()]; paint.getTextWidths(mText, measuredWidth); if (mDotsTextWidth == 0.0f) { mDotsTextWidth = paint.measureText(mDotsText); } float currentWidth = 0.0f; int index = measuredWidth.length; for (int i = 0; i < measuredWidth.length; i++) { if (currentWidth < maxWidth - mDotsTextWidth) { currentWidth += measuredWidth[i]; } else { index = i - 1; break; } } if (index == measuredWidth.length || index == -1) { InnerTextView.this.setText(mText); if (!InnerTextView.this.isLayoutRequested()) { InnerTextView.this.requestLayout(); } } else { Spanned str = Html.fromHtml("<u>" + mDotsText + "</u>"); InnerTextView.this.setText(mText.substring(0, index) + str); if (!InnerTextView.this.isLayoutRequested()) { InnerTextView.this.requestLayout(); } } }
然后我们处理一下点击事件
@Override public boolean onTouchEvent(MotionEvent event) { final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: { break; } case MotionEvent.ACTION_UP: { if (mLinesable && event.getX() > this.getWidth() - (mAreaRatio * mDotsTextWidth) && event.getX() < this.getWidth()) { mLinesable = false; mIsClicked = true; InnerTextView.this.setText(mText); } break; } default: break; } return true; }
初始化这个控件的时候:
private void intSubView() { mText = ""; mIsClicked = false; mDotsText = this.getContext().getResources().getString(R.string.expandable_text_dots); mInnerTextView = new InnerTextView(getContext()); if (mGravity != -1) { switch (mGravity) { case 0: mInnerTextView.setGravity(Gravity.LEFT); break; case 1: mInnerTextView.setGravity(Gravity.CENTER); break; case 2: mInnerTextView.setGravity(Gravity.RIGHT); break; } } mInnerTextView.setText(mText); if (BOLDSTYLE.equals(mTextStyle)) { mInnerTextView.getPaint().setFakeBoldText(true); } mInnerTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSizePixel); mInnerTextView.setTextColor(mTextColor); if (mMaxLines != -1) { mInnerTextView.setMaxLines(mMaxLines); } this.addView(mInnerTextView); ViewGroup.LayoutParams lp = mInnerTextView.getLayoutParams(); lp.width = ViewGroup.LayoutParams.MATCH_PARENT; lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦