package com.sahadev.sildingfinishlayout; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Scroller; import android.widget.TextView; public class ExpandableTextView extends RelativeLayout { private TextView mTextView; private Button mButton; private int mTextViewId = 567576458;// 这里注意不要随便填一个简单的数字,可能会和R中的ID冲突造成无效 private Scroller mScroller; private int mHeight, mWidthMeasureSpec, mButtonHeight, paddingSize = 1; private boolean isExpanded, WSettedFlag, HSettedFlag, onceFlag; private int times = 2;// 缩小的倍数,默认2倍 public ExpandableTextView(Context context) { this(context, null); } public ExpandableTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mTextView = new TextView(context); mTextView.setId(mTextViewId); mButton = new Button(context); mButton.setText("扩 展"); mScroller = new Scroller(context); LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.BELOW, mTextViewId); lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!isExpanded) { // 展开来 mScroller.startScroll(0, mHeight / times, 0, mHeight / times); postInvalidate(); isExpanded = true; } else { // 收回去 mScroller.startScroll(0, mHeight, 0, -mHeight / times); postInvalidate(); isExpanded = false; } } }); addView(mTextView); addView(mButton, lp); } @Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { mTextView.setHeight(mScroller.getCurrY()); postInvalidate(); return; } } public void setTimes(int times) { if (times == 0) { throw new ArithmeticException("倍数不能为0"); } this.times = times; } public void setTextViewPadding(int pixels) { mTextView.setPadding(pixels, pixels, pixels, 0); paddingSize = pixels; } public void setButtonTips(CharSequence text) { mButton.setText(text); } public void setText(CharSequence text) { mTextView.setText(text); } public void setTextColor(int color) { mTextView.setTextColor(color); } public void setTextSize(float size) { mTextView.setTextSize(size); } public void setBackgroundColor(int color) { mTextView.setBackgroundColor(color); } public void setWidth(int width) { mWidthMeasureSpec = width; mTextView.setWidth(width - paddingSize * 2); WSettedFlag = true; } public void setHeight(int height) { mHeight = (height - mButtonHeight) * 2; HSettedFlag = true; } /* onMeasure方法在重绘的时候会一直被调用 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); /* 此步骤只用执行一次,获取到textView的宽度以及Button的高度以及一些初始化的值 */ if (!WSettedFlag) { WSettedFlag = true; mWidthMeasureSpec = mTextView.getMeasuredWidth(); } if (!HSettedFlag) { HSettedFlag = true; mHeight = mTextView.getMeasuredHeight(); OtherTools.showLog("mHeight----" + mHeight); } if (!onceFlag) { onceFlag = true; mButtonHeight = mButton.getMeasuredHeight(); OtherTools.showLog("mButtonHeight----" + mButtonHeight); // mTextView.setHeight(mHeight / times + mButtonHeight > // heightMeasureSpec ? heightMeasureSpec - mButtonHeight : mHeight / // times); mTextView.setHeight(mHeight / times); } // int tempHeight = mHeight / 2 + mButtonHeight; // tempHeight = tempHeight > heightMeasureSpec ? heightMeasureSpec : // tempHeight; setMeasuredDimension(mWidthMeasureSpec, mButtonHeight + mTextView.getMeasuredHeight()); } }