很多时候由于手机屏幕的显示,TextView单行没法显示完全内容。如下所示:
当点击3个点的时候,文字就会展开:
对于这个需求,可以使用一个LinearLayout,orientation是horizontal,有两个TextView,一个显示内容,一个显示...
在ClickableTextVIew这个类里,我们先取到xml的属性:
public ClickableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ClickableTextView, 0, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.ClickableTextView_textSize:
mTextSizePixel = a.getDimension(attr, DEFAULT_TEXT_SIZE);
break;
case R.styleable.ClickableTextView_textColor:
mTextColor = a.getColor(attr, DEFAULT_TEXT_COLOR);
break;
case R.styleable.ClickableTextView_textStyle:
mTextStyle = a.getString(R.styleable.ClickableTextView_textStyle);
break;
case R.styleable.ClickableTextView_text:
defaultText = a.getString(R.styleable.ClickableTextView_text);
break;
}
}
a.recycle();
init(context);
}
private void init(Context context) {
if(mRoot == null) {
mRoot = inflate(context, R.layout.customtext, this);
}
mRoot.setFocusable(true);
mRoot.setFocusableInTouchMode(true);
mDots = (TextView) findViewById(R.id.mainDots);
mDots.setVisibility(View.GONE);
mDots.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSizePixel);
//mDots.setTextSize(mTextSize);
mDots.setTextColor(mTextColor);
mDots.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //
mDots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mExpanded)
onDotsClicked();
}
});
mText = (TextView) findViewById(R.id.mainText);
if (BOLDSTYLE.equals(mTextStyle)) {
mText.getPaint().setFakeBoldText(true);
}
mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSizePixel);
//mText.setTextSize(mTextSize);
mText.setTextColor(mTextColor);
}
在onSizeChange的时候,这个view已经得到宽度了,这时我们把文字放入textview中:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int width = w;
mTextWidth = width -3*getDotsWidth();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mTextWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
mText.setSingleLine(true);
mText.setLayoutParams(params);
if(!TextUtils.isEmpty(defaultText)) {
setText(defaultText);
}
}
public void setWidth(int paddingLeft){
if(mIsSetWidth){
return;
}
mIsSetWidth = true;
this.mTextWidth = (mTextWidth-paddingLeft);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mTextWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
mText.setSingleLine(true);
mText.setLayoutParams(params);
}
private int getDotsWidth() {
TextPaint mPaint = mDots.getPaint();
return (int)mPaint.measureText(mDots.getText().toString());
}
public void setText(String text) {
if(mIsClicked){
return;
}
if(text == null) {
text = "";
}
mContent = text;
mExpanded = false;
mOriText = text;
mText.setText(mContent);
mText.measure(getMeasuredWidth(), getMeasuredHeight());
mDots.setVisibility(View.GONE);
String newText = getTruncText(mContent, mText);
if (newText.length() < mText.length()) {
mDots.setVisibility(View.VISIBLE);
mText.measure(getMeasuredWidth(), getMeasuredHeight());
newText = getTruncText(mContent, mText);
mText.setText(newText);
}
} private String getTruncText(String text, TextView view) {
final Paint textPaint = view.getPaint();
final int numChar = textPaint.breakText(text, true, mTextWidth, null);
return text.substring(0, numChar);
}
可以在http://download.csdn.net/detail/baidu_nod/7521021下载代码