万能的TextView,实现常用布局样式
package com.loaderman.textviewdemo; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; /** * 通用的textView可以实现大部分常用布局样式 */ public class CommonTextView extends RelativeLayout { private Context mContext; private int defaultColor = 0xFF373737;//文字默认颜色 private int defaultDividerLineColor = 0xFFE8E8E8;//分割线默认颜色 private int defaultSize;//默认字体大小 private int defaultPadding;//默认边距 private int mBackgroundColor = 0xffffffff;//背景颜色 private Drawable mLeft_drawableLeft; private Drawable mLeft_drawableTop; private Drawable mLeft_drawableRight; private Drawable mLeft_drawableBottom; private Drawable mCenter_drawableLeft; private Drawable mCenter_drawableTop; private Drawable mCenter_drawableRight; private Drawable mCenter_drawableBottom; private Drawable mRight_drawableLeft; private Drawable mRight_drawableTop; private Drawable mRight_drawableRight; private Drawable mRight_drawableBottom; private Drawable mLeft_IV_drawable; // private Drawable mRight_IV_drawable; private CharSequence mLeftTextString; private CharSequence mLeftTopTextString; private CharSequence mLeftBottomTextString; private CharSequence mRightTextString; private CharSequence mRightTopTextString; private CharSequence mRightBottomTextString; private CharSequence mCenterTextString; private CharSequence mCenterTopTextString; private CharSequence mCenterBottomTextString; private int mLeftTextSize; private int mLeftTopTextSize; private int mLeftBottomTextSize; private int mRightTextSize; private int mRightTopTextSize; private int mRightBottomTextSize; private int mCenterTextSize; private int mCenterTopTextSize; private int mCenterBottomTextSize; private int mLeftTextColor; private int mLeftTopTextColor; private int mLeftBottomTextColor; private int mCenterTextColor; private int mCenterTopTextColor; private int mCenterBottomTextColor; private int mRightTextColor; private int mRightTopTextColor; private int mRightBottomTextColor; private int mLeftIconDrawablePadding; private int mCenterIconDrawablePadding; private int mRightIconDrawablePadding; private int mLeftViewPaddingLeft; private int mLeftViewPaddingRight; private int mCenterViewPaddingLeft; private int mCenterViewPaddingRight; private int mRightViewPaddingLeft; private int mRightViewPaddingRight; private int mTopDividerLineMarginLR; private int mTopDividerLineMarginLeft; private int mTopDividerLineMarginRight; private int mBottomDividerLineMarginLR; private int mBottomDividerLineMarginLeft; private int mBottomDividerLineMarginRight; private int mBothDividerLineMarginLeft; private int mBothDividerLineMarginRight; private int mCenterSpaceHeight; private int mLeftImageViewMarginLeft; // private int mRightImageViewMarginRight; private int mDividerLineType; private int mDividerLineColor; private int mDividerLineHeight; private int mLeftTextViewLineSpacingExtra; private int mCenterTextViewLineSpacingExtra; private int mRightTextViewLineSpacingExtra; /** * 分割线的类型 */ private static final int NONE = 0; private static final int TOP = 1; private static final int BOTTOM = 2; private static final int BOTH = 3; private static final int DEFAULT = BOTTOM; /** * 是否使用点击出现波纹效果 */ private boolean useRipple; private boolean mSetSingleLine = true; private int mSetMaxEms = 10; private int mSetLines = 1; /** * TextView的Gravity */ private static final int Gravity_Left_Center = 0; private static final int Gravity_Center = 1; private static final int Gravity_Right_Center = 2; private static final int DEFAULT_Gravity = 1; private int mLeftTextViewGravity; private int mCenterTextViewGravity; private int mRightTextViewGravity; private TextView leftTextView, centerTextView, rightTextView; private TextView leftTopTextView, centerTopTextView, rightTopTextView; private TextView leftBottomTextView, centerBottomTextView, rightBottomTextView; private ImageView leftImageView; // private ImageView rightImageView; private View topLineView, bottomLineView; private View centerBaseLineView; private boolean mLeftViewIsClickable = false; private boolean mCenterViewIsClickable = false; private boolean mRightViewIsClickable = false; private RelativeLayout.LayoutParams leftTVParams, centerTVParams, rightTVParams, topLineParams, bottomLineParams; private RelativeLayout.LayoutParams leftTopTVParams, centerTopTVParams, rightTopTVParams; private RelativeLayout.LayoutParams leftBottomTVParams, centerBottomTVParams, rightBottomTVParams; private RelativeLayout.LayoutParams centerBaseLineParams; private RelativeLayout.LayoutParams leftIVParams; // private RelativeLayout.LayoutParams rightIVParams; private OnCommonTextViewClickListener onCommonTextViewClickListener; private Drawable mBackground_drawable; private boolean mIsCenterAlignLeft = false; private int mCenterViewMarginLeft; public CommonTextView(Context context) { this(context, null); } public CommonTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CommonTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; defaultSize = dip2px(context, 13); defaultPadding = dip2px(context, 10); mCenterSpaceHeight = dip2px(context, 5); getAttr(attrs); init(); } /** * 获取自定义控件资源 * * @param attrs attrs */ private void getAttr(AttributeSet attrs) { TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CommonTextView); ////////设置文字或者图片资源//////// mLeft_drawableLeft = typedArray.getDrawable(R.styleable.CommonTextView_cLeftIconResForDrawableLeft); mLeft_drawableTop = typedArray.getDrawable(R.styleable.CommonTextView_cLeftIconResForDrawableTop); mLeft_drawableRight = typedArray.getDrawable(R.styleable.CommonTextView_cLeftIconResForDrawableRight); mLeft_drawableBottom = typedArray.getDrawable(R.styleable.CommonTextView_cLeftIconResForDrawableBottom); mCenter_drawableLeft = typedArray.getDrawable(R.styleable.CommonTextView_cCenterIconResForDrawableLeft); mCenter_drawableTop = typedArray.getDrawable(R.styleable.CommonTextView_cCenterIconResForDrawableTop); mCenter_drawableRight = typedArray.getDrawable(R.styleable.CommonTextView_cCenterIconResForDrawableRight); mCenter_drawableBottom = typedArray.getDrawable(R.styleable.CommonTextView_cCenterIconResForDrawableBottom); mRight_drawableLeft = typedArray.getDrawable(R.styleable.CommonTextView_cRightIconResForDrawableLeft); mRight_drawableTop = typedArray.getDrawable(R.styleable.CommonTextView_cRightIconResForDrawableTop); mRight_drawableRight = typedArray.getDrawable(R.styleable.CommonTextView_cRightIconResForDrawableRight); mRight_drawableBottom = typedArray.getDrawable(R.styleable.CommonTextView_cRightIconResForDrawableBottom); mLeft_IV_drawable = typedArray.getDrawable(R.styleable.CommonTextView_cLeftImageViewDrawableRes); // mRight_IV_drawable = typedArray.getDrawable(R.styleable.CommonTextView_cRightImageViewDrawableRes); ///////////////////// mLeftTextString = typedArray.getString(R.styleable.CommonTextView_cLeftTextString); mLeftTopTextString = typedArray.getString(R.styleable.CommonTextView_cLeftTopTextString); mLeftBottomTextString = typedArray.getString(R.styleable.CommonTextView_cLeftBottomTextString); mCenterTextString = typedArray.getString(R.styleable.CommonTextView_cCenterTextString); mCenterTopTextString = typedArray.getString(R.styleable.CommonTextView_cCenterTopTextString); mCenterBottomTextString = typedArray.getString(R.styleable.CommonTextView_cCenterBottomTextString); mRightTextString = typedArray.getString(R.styleable.CommonTextView_cRightTextString); mRightTopTextString = typedArray.getString(R.styleable.CommonTextView_cRightTopTextString); mRightBottomTextString = typedArray.getString(R.styleable.CommonTextView_cRightBottomTextString); mLeftTextColor = typedArray.getColor(R.styleable.CommonTextView_cLeftTextColor, defaultColor); mLeftTopTextColor = typedArray.getColor(R.styleable.CommonTextView_cLeftTopTextColor, defaultColor); mLeftBottomTextColor = typedArray.getColor(R.styleable.CommonTextView_cLeftBottomTextColor, defaultColor); mCenterTextColor = typedArray.getColor(R.styleable.CommonTextView_cCenterTextColor, defaultColor); mCenterTopTextColor = typedArray.getColor(R.styleable.CommonTextView_cCenterTopTextColor, defaultColor); mCenterBottomTextColor = typedArray.getColor(R.styleable.CommonTextView_cCenterBottomTextColor, defaultColor); mRightTextColor = typedArray.getColor(R.styleable.CommonTextView_cRightTextColor, defaultColor); mRightTopTextColor = typedArray.getColor(R.styleable.CommonTextView_cRightTopTextColor, defaultColor); mRightBottomTextColor = typedArray.getColor(R.styleable.CommonTextView_cRightBottomTextColor, defaultColor); mLeftTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftTextSize, defaultSize); mLeftTopTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftTopTextSize, defaultSize); mLeftBottomTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftBottomTextSize, defaultSize); mCenterTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterTextSize, defaultSize); mCenterTopTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterTopTextSize, defaultSize); mCenterBottomTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterBottomTextSize, defaultSize); mRightTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightTextSize, defaultSize); mRightTopTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightTopTextSize, defaultSize); mRightBottomTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightBottomTextSize, defaultSize); mLeftIconDrawablePadding = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftIconDrawablePadding, defaultPadding); mCenterIconDrawablePadding = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterIconDrawablePadding, defaultPadding); mRightIconDrawablePadding = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightIconDrawablePadding, defaultPadding); mLeftViewPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftViewPaddingLeft, defaultPadding); mLeftViewPaddingRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftViewPaddingRight, defaultPadding); mCenterViewPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterViewPaddingLeft, defaultPadding); mCenterViewPaddingRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterViewPaddingRight, defaultPadding); mRightViewPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightViewPaddingLeft, defaultPadding); mRightViewPaddingRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightViewPaddingRight, defaultPadding); mBothDividerLineMarginLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBothDividerLineMarginLeft, 0); mBothDividerLineMarginRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBothDividerLineMarginRight, 0); mTopDividerLineMarginLR = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cTopDividerLineMarginLR, 0); mTopDividerLineMarginLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cTopDividerLineMarginLeft, 0); mTopDividerLineMarginRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cTopDividerLineMarginRight, 0); mBottomDividerLineMarginLR = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBottomDividerLineMarginLR, 0); mBottomDividerLineMarginLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBottomDividerLineMarginLeft, 0); mBottomDividerLineMarginRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBottomDividerLineMarginRight, 0); mLeftImageViewMarginLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftImageViewMarginLeft, defaultPadding); // mRightImageViewMarginRight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightImageViewMarginRight, defaultPadding); mCenterSpaceHeight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterSpaceHeight, mCenterSpaceHeight); mLeftTextViewLineSpacingExtra = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftTextViewLineSpacingExtra, 0); mCenterTextViewLineSpacingExtra = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterTextViewLineSpacingExtra, 0); mRightTextViewLineSpacingExtra = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightTextViewLineSpacingExtra, 0); mDividerLineType = typedArray.getInt(R.styleable.CommonTextView_cShowDividerLineType, DEFAULT); mDividerLineColor = typedArray.getColor(R.styleable.CommonTextView_cDividerLineColor, defaultDividerLineColor); mDividerLineHeight = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cDividerLineHeight, dip2px(mContext, 0.5f)); useRipple = typedArray.getBoolean(R.styleable.CommonTextView_cUseRipple, false); mBackgroundColor = typedArray.getColor(R.styleable.CommonTextView_cBackgroundColor, mBackgroundColor); mSetSingleLine = typedArray.getBoolean(R.styleable.CommonTextView_cSetSingleLine, true); mSetMaxEms = typedArray.getInt(R.styleable.CommonTextView_cSetMaxEms, mSetMaxEms); mSetLines = typedArray.getInt(R.styleable.CommonTextView_cSetLines, 1); mLeftTextViewGravity = typedArray.getInt(R.styleable.CommonTextView_cLeftTextViewGravity, DEFAULT_Gravity); mCenterTextViewGravity = typedArray.getInt(R.styleable.CommonTextView_cCenterTextViewGravity, DEFAULT_Gravity); mRightTextViewGravity = typedArray.getInt(R.styleable.CommonTextView_cRightTextViewGravity, DEFAULT_Gravity); mLeftViewIsClickable = typedArray.getBoolean(R.styleable.CommonTextView_cLeftViewIsClickable, false); mCenterViewIsClickable = typedArray.getBoolean(R.styleable.CommonTextView_cCenterViewIsClickable, false); mRightViewIsClickable = typedArray.getBoolean(R.styleable.CommonTextView_cRightViewIsClickable, false); mBackground_drawable = typedArray.getDrawable(R.styleable.CommonTextView_cBackgroundDrawableRes); mIsCenterAlignLeft = typedArray.getBoolean(R.styleable.CommonTextView_cIsCenterAlignLeft, false); mCenterViewMarginLeft = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterViewMarginLeft, dip2px(mContext, 200)); typedArray.recycle(); } /** * 初始化 */ private void init() { initCommonTextView(); initLineView(); initCenterBaseLine(); if (mLeft_IV_drawable != null) { initLeftImageView(); } // if (mRight_IV_drawable!=null){ // initRightImageView(); // } if (mLeftTextString != null || mLeft_drawableLeft != null || mLeft_drawableRight != null) { initLeftText(); } if (mCenterTextString != null) { initCenterText(); } if (mRightTextString != null || mRight_drawableLeft != null || mRight_drawableRight != null) { initRightText(); } if (mLeftTopTextString != null) { initLeftTopText(); } if (mLeftBottomTextString != null) { initLeftBottomText(); } if (mCenterTopTextString != null) { initCenterTopText(); } if (mCenterBottomTextString != null) { initCenterBottomText(); } if (mRightTopTextString != null) { initRightTopText(); } if (mRightBottomTextString != null) { initRightBottomText(); } } /** * 初始化commonTextView */ private void initCommonTextView() { this.setBackgroundColor(mBackgroundColor); if (useRipple) { this.setBackgroundResource(R.drawable.selector_white); } this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onCommonTextViewClick(); } } }); if (mBackground_drawable != null) { this.setBackgroundDrawable(mBackground_drawable); } } /** * 为了设置上下两排文字居中对齐显示而需要设置的基准线 */ private void initCenterBaseLine() { if (centerBaseLineView == null) { if (centerBaseLineParams == null) { centerBaseLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, mCenterSpaceHeight); centerBaseLineParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); } centerBaseLineView = new View(mContext); centerBaseLineView.setId(R.id.cCenterBaseLineId); centerBaseLineView.setLayoutParams(centerBaseLineParams); } addView(centerBaseLineView); } /** * 初始化分割线 */ private void initLineView() { switch (mDividerLineType) { case NONE: break; case TOP: setTopLineMargin(); break; case BOTTOM: setBottomLineMargin(); break; case BOTH: setTopLineMargin(); setBottomLineMargin(); break; } } /** * 设置顶部的分割线 */ private void setTopLineMargin() { if (mTopDividerLineMarginLR != 0) { initTopLineView(mTopDividerLineMarginLR, mTopDividerLineMarginLR); } else if (mBothDividerLineMarginLeft != 0 | mBothDividerLineMarginRight != 0) { initTopLineView(mBothDividerLineMarginLeft, mBothDividerLineMarginRight); } else { initTopLineView(mTopDividerLineMarginLeft, mTopDividerLineMarginRight); } } /** * 设置底部的分割线 */ private void setBottomLineMargin() { if (mBottomDividerLineMarginLR != 0) { initBottomLineView(mBottomDividerLineMarginLR, mBottomDividerLineMarginLR); } else if (mBothDividerLineMarginRight != 0 | mBothDividerLineMarginRight != 0) { initBottomLineView(mBothDividerLineMarginLeft, mBothDividerLineMarginRight); } else { initBottomLineView(mBottomDividerLineMarginLeft, mBottomDividerLineMarginRight); } } /** * 设置上边分割线view * * @param marginLeft 左间距 * @param marginRight 右间距 */ private void initTopLineView(int marginLeft, int marginRight) { if (topLineView == null) { if (topLineParams == null) { topLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, mDividerLineHeight); } topLineParams.addRule(ALIGN_PARENT_TOP, TRUE); topLineParams.setMargins(marginLeft, 0, marginRight, 0); topLineView = new View(mContext); topLineView.setLayoutParams(topLineParams); topLineView.setBackgroundColor(mDividerLineColor); } addView(topLineView); } /** * 设置底部分割线view * * @param marginLeft 左间距 * @param marginRight 右间距 */ private void initBottomLineView(int marginLeft, int marginRight) { if (bottomLineView == null) { if (bottomLineParams == null) { bottomLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, mDividerLineHeight); } bottomLineParams.addRule(ALIGN_PARENT_BOTTOM, TRUE); bottomLineParams.setMargins(marginLeft, 0, marginRight, 0); bottomLineView = new View(mContext); bottomLineView.setLayoutParams(bottomLineParams); bottomLineView.setBackgroundColor(mDividerLineColor); } addView(bottomLineView); } /** * 初始化左边ImageView * 主要是为了便于使用第三方图片框架获取网络图片使用 */ private void initLeftImageView() { leftImageView = new ImageView(mContext); leftIVParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftIVParams.addRule(ALIGN_PARENT_LEFT, TRUE); leftIVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); leftIVParams.setMargins(mLeftImageViewMarginLeft, 0, 0, 0); leftImageView.setScaleType(ImageView.ScaleType.CENTER); leftImageView.setId(R.id.cLeftImageViewId); leftImageView.setLayoutParams(leftIVParams); if (mLeft_IV_drawable != null) { leftImageView.setImageDrawable(mLeft_IV_drawable); } addView(leftImageView); } /** * 初始化右边ImageView * 主要是为了便于使用第三方图片框架获取网络图片使用 */ // private void initRightImageView() { // rightImageView = new ImageView(mContext); // rightIVParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // rightIVParams.addRule(ALIGN_PARENT_RIGHT, TRUE); // rightIVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); // rightIVParams.setMargins(0, 0, mRightImageViewMarginRight, 0); // rightImageView.setScaleType(ImageView.ScaleType.CENTER); // rightImageView.setId(R.id.cRightImageViewId); // rightImageView.setLayoutParams(rightIVParams); // if (mRight_IV_drawable != null) { // rightImageView.setImageDrawable(mRight_IV_drawable); // } // addView(rightImageView); // } /** * 初始化左边textView */ private void initLeftText() { if (leftTextView == null) { if (leftTVParams == null) { leftTVParams = getParams(leftTVParams); } leftTVParams.addRule(CENTER_VERTICAL, TRUE); leftTVParams.addRule(RIGHT_OF, R.id.cLeftImageViewId); leftTVParams.setMargins(mLeftViewPaddingLeft, 0, mLeftViewPaddingRight, 0); leftTextView = initText(leftTextView, leftTVParams, R.id.cLeftTextId, mLeftTextColor, mLeftTextSize); leftTextView.setText(mLeftTextString); leftTextView.setLineSpacing(mLeftTextViewLineSpacingExtra, 1.0f); setTextViewGravity(leftTextView, mLeftTextViewGravity); if (mLeftViewIsClickable) { leftTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onLeftViewClick(); } } }); } } setDrawable(leftTextView, mLeft_drawableLeft, mLeft_drawableTop, mLeft_drawableRight, mLeft_drawableBottom, mLeftIconDrawablePadding); } /** * 初始化左上textView */ private void initLeftTopText() { if (leftTopTextView == null) { if (leftTopTVParams == null) { leftTopTVParams = getParams(leftTopTVParams); } leftTopTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); leftTopTVParams.addRule(ABOVE, R.id.cCenterBaseLineId); leftTopTVParams.addRule(RIGHT_OF, R.id.cLeftImageViewId); leftTopTVParams.setMargins(mLeftViewPaddingLeft, 0, mLeftViewPaddingRight, 0); leftTopTextView = initText(leftTopTextView, leftTopTVParams, R.id.cLeftTopTextId, mLeftTopTextColor, mLeftTopTextSize); leftTopTextView.setText(mLeftTopTextString); setTextViewGravity(leftTopTextView, mLeftTextViewGravity); } } /** * 初始化左下textView */ private void initLeftBottomText() { if (leftBottomTextView == null) { if (leftBottomTVParams == null) { leftBottomTVParams = getParams(leftBottomTVParams); } leftBottomTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); leftBottomTVParams.addRule(BELOW, R.id.cCenterBaseLineId); leftBottomTVParams.addRule(RIGHT_OF, R.id.cLeftImageViewId); leftBottomTVParams.setMargins(mLeftViewPaddingLeft, 0, mLeftViewPaddingRight, 0); leftBottomTextView = initText(leftBottomTextView, leftBottomTVParams, R.id.cLeftBottomTextId, mLeftBottomTextColor, mLeftBottomTextSize); leftBottomTextView.setText(mLeftBottomTextString); setTextViewGravity(leftBottomTextView, mLeftTextViewGravity); } } /** * 初始化中间textView */ private void initCenterText() { if (centerTextView == null) { if (centerTVParams == null) { if (mIsCenterAlignLeft) { centerTVParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); } else { centerTVParams = getParams(centerTVParams); } } centerTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); centerTVParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); if (mIsCenterAlignLeft) { centerTextView = initText(centerTextView, centerTVParams, R.id.cCenterTextId, mCenterTextColor, mCenterTextSize); centerTVParams.setMargins(mCenterViewMarginLeft, 0, mCenterViewPaddingRight, 0); setTextViewGravity(centerTextView, Gravity_Left_Center); } else { centerTextView = initText(centerTextView, centerTVParams, R.id.cCenterTextId, mCenterTextColor, mCenterTextSize); centerTVParams.setMargins(mCenterViewPaddingLeft, 0, mCenterViewPaddingRight, 0); setTextViewGravity(centerTextView, mCenterTextViewGravity); } centerTextView.setText(mCenterTextString); centerTextView.setLineSpacing(mCenterTextViewLineSpacingExtra, 1.0f); if (mCenterViewIsClickable) { centerTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onCenterViewClick(); } } }); } } setDrawable(centerTextView, mCenter_drawableLeft, mCenter_drawableTop, mCenter_drawableRight, mCenter_drawableBottom, mCenterIconDrawablePadding); } /** * 初始化中上textView */ private void initCenterTopText() { if (centerTopTextView == null) { if (centerTopTVParams == null) { centerTopTVParams = getParams(centerTopTVParams); } centerTopTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); centerTopTVParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); centerTopTVParams.addRule(ABOVE, R.id.cCenterBaseLineId); centerTopTVParams.setMargins(mCenterViewPaddingLeft, 0, mCenterViewPaddingRight, 0); centerTopTextView = initText(centerTopTextView, centerTopTVParams, R.id.cCenterTopTextId, mCenterTopTextColor, mCenterTopTextSize); centerTopTextView.setText(mCenterTopTextString); centerTopTextView.setLineSpacing(mCenterTextViewLineSpacingExtra, 1.0f); setTextViewGravity(centerTopTextView, mCenterTextViewGravity); } } /** * 初始化中下textView */ private void initCenterBottomText() { if (centerBottomTextView == null) { if (centerBottomTVParams == null) { centerBottomTVParams = getParams(centerBottomTVParams); } centerBottomTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); centerBottomTVParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); centerBottomTVParams.addRule(BELOW, R.id.cCenterBaseLineId); centerBottomTVParams.setMargins(mCenterViewPaddingLeft, 0, mCenterViewPaddingRight, 0); centerBottomTextView = initText(centerBottomTextView, centerBottomTVParams, R.id.cCenterBottomTextId, mCenterBottomTextColor, mCenterBottomTextSize); centerBottomTextView.setText(mCenterBottomTextString); centerBottomTextView.setLineSpacing(mCenterTextViewLineSpacingExtra, 1.0f); setTextViewGravity(centerBottomTextView, mCenterTextViewGravity); } } /** * 初始化右边textView */ private void initRightText() { if (rightTextView == null) { if (rightTVParams == null) { rightTVParams = getParams(rightTVParams); } rightTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); rightTVParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); rightTVParams.addRule(RelativeLayout.LEFT_OF, R.id.cRightImageViewId); rightTVParams.setMargins(mRightViewPaddingLeft, 0, mRightViewPaddingRight, 0); rightTextView = initText(rightTextView, rightTVParams, R.id.cRightTextId, mRightTextColor, mRightTextSize); rightTextView.setText(mRightTextString); // rightTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT); rightTextView.setLineSpacing(mRightTextViewLineSpacingExtra, 1.0f); setTextViewGravity(rightTextView, mRightTextViewGravity); if (mRightViewIsClickable) { rightTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onRightViewClick(); } } }); } } setDrawable(rightTextView, mRight_drawableLeft, mRight_drawableTop, mRight_drawableRight, mRight_drawableBottom, mRightIconDrawablePadding); } /** * 初始化右上textView */ private void initRightTopText() { if (rightTopTextView == null) { if (rightTopTVParams == null) { rightTopTVParams = getParams(rightTopTVParams); } rightTopTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); rightTopTVParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); rightTopTVParams.addRule(ABOVE, R.id.cCenterBaseLineId); rightTopTVParams.addRule(RelativeLayout.LEFT_OF, R.id.cRightImageViewId); rightTopTVParams.setMargins(mRightViewPaddingLeft, 0, mRightViewPaddingRight, 0); rightTopTextView = initText(rightTopTextView, rightTopTVParams, R.id.cRightTopTextId, mRightTopTextColor, mRightTopTextSize); rightTopTextView.setText(mRightTopTextString); rightTopTextView.setLineSpacing(mRightTextViewLineSpacingExtra, 1.0f); setTextViewGravity(rightTopTextView, mRightTextViewGravity); } } /** * 初始化右下textView */ private void initRightBottomText() { if (rightBottomTextView == null) { if (rightBottomTVParams == null) { rightBottomTVParams = getParams(rightBottomTVParams); } rightBottomTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); rightBottomTVParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); rightBottomTVParams.addRule(BELOW, R.id.cCenterBaseLineId); rightBottomTVParams.addRule(RelativeLayout.LEFT_OF, R.id.cRightImageViewId); rightBottomTVParams.setMargins(mRightViewPaddingLeft, 0, mRightViewPaddingRight, 0); rightBottomTextView = initText(rightBottomTextView, rightBottomTVParams, R.id.cRightBottomTextId, mRightBottomTextColor, mRightBottomTextSize); rightBottomTextView.setText(mRightBottomTextString); rightBottomTextView.setLineSpacing(mRightTextViewLineSpacingExtra, 1.0f); setTextViewGravity(rightBottomTextView, mRightTextViewGravity); } } /** * 初始化Params * * @param params 对象 * @return 返回 */ public LayoutParams getParams(LayoutParams params) { if (params == null) { params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } return params; } /** * 初始化textView * * @param textView 对象 * @param layoutParams 对象 * @param id id * @param textColor 颜色值 * @param textSize 字体大小 * @return 返回 */ public TextView initText(TextView textView, LayoutParams layoutParams, int id, int textColor, int textSize) { if (textView == null) { textView = new TextView(mContext); textView.setId(id); textView.setLayoutParams(layoutParams); textView.setTextColor(textColor); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); // textView.setGravity(Gravity.CENTER); textView.setLines(mSetLines); textView.setSingleLine(mSetSingleLine); textView.setMaxEms(mSetMaxEms); textView.setEllipsize(TextUtils.TruncateAt.END); addView(textView); } return textView; } private void setTextViewGravity(TextView textView, int gravity_type) { switch (gravity_type) { case Gravity_Left_Center: textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); break; case Gravity_Center: textView.setGravity(Gravity.CENTER); break; case Gravity_Right_Center: textView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); } } /** * 设置textView的drawable * * @param textView 对象 * @param drawableLeft 左边图标 * @param drawableTop 上边图标 * @param drawableRight 右边图标 * @param drawableBottom 下边图标 * @param drawablePadding 图标距离文字的间距 */ public void setDrawable(TextView textView, Drawable drawableLeft, Drawable drawableTop, Drawable drawableRight, Drawable drawableBottom, int drawablePadding) { textView.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); textView.setCompoundDrawablePadding(drawablePadding); } /** * 设置左边文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setLeftTextString(CharSequence string) { if (leftTextView == null) { initLeftText(); } leftTextView.setText(string); return this; } /** * 设置左上文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setLeftTopTextString(CharSequence string) { if (leftTopTextView == null) { initLeftTopText(); } leftTopTextView.setText(string); return this; } /** * 设置左下文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setLeftBottomTextString(CharSequence string) { if (leftBottomTextView == null) { initLeftBottomText(); } leftBottomTextView.setText(string); return this; } /** * 设置左边文字字体大小 * * @param size 字体大小 * @return 返回 */ public CommonTextView setLeftTextSize(float size) { if (leftTextView == null) { initLeftText(); } leftTextView.setTextSize(size); return this; } /** * set Left TextColor * * @param color textColor * @return return */ public CommonTextView setLeftTextColor(int color) { if (leftTextView == null) { initLeftText(); } leftTextView.setTextColor(color); return this; } /** * 设置中间文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setCenterTextString(CharSequence string) { if (centerTextView == null) { initCenterText(); } centerTextView.setText(string); return this; } /** * 设置中上文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setCenterTopTextString(CharSequence string) { if (centerTopTextView == null) { initCenterTopText(); } centerTopTextView.setText(string); return this; } /** * 设置中下文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setCenterBottomTextString(CharSequence string) { if (centerBottomTextView == null) { initCenterBottomText(); } centerBottomTextView.setText(string); return this; } /** * 设置中间文字字体大小 * * @param size 字体大小 * @return 返回 */ public CommonTextView setCenterTextSize(float size) { if (centerTextView == null) { initCenterText(); } centerTextView.setTextSize(size); return this; } /** * 设置中间文字颜色值 * * @param color 颜色值 * @return 返回 */ public CommonTextView setCenterTextColor(int color) { if (centerTextView == null) { initCenterText(); } centerTextView.setTextColor(color); return this; } /** * 设置右边文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setRightTextString(CharSequence string) { if (rightTextView == null) { initRightText(); } rightTextView.setText(string); return this; } /** * 设置右上文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setRightTopTextString(CharSequence string) { if (rightTopTextView == null) { initRightTopText(); } rightTopTextView.setText(string); return this; } /** * 设置右下文字字符串 * * @param string 字符串 * @return 返回 */ public CommonTextView setRightBottomTextString(CharSequence string) { if (rightBottomTextView == null) { initRightBottomText(); } rightBottomTextView.setText(string); return this; } /** * 设置右边文字的字体大小 * * @param size 字体大小 * @return 返回 */ public CommonTextView setRightTextSize(float size) { if (rightTextView == null) { initRightText(); } rightTextView.setTextSize(size); return this; } /** * 设置右边文字的颜色 * * @param color 颜色值 * @return 返回 */ public CommonTextView setRightTextColor(int color) { if (rightTextView == null) { initRightText(); } rightTextView.setTextColor(color); return this; } /** * 设置左边view的drawableLeft * * @param drawableLeft 资源 * @return 返回 */ public CommonTextView setLeftDrawableLeft(Drawable drawableLeft) { if (drawableLeft != null) { drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight()); } if (leftTextView == null) { initLeftText(); } leftTextView.setCompoundDrawables(drawableLeft, null, null, null); return this; } /** * 设置左边view的drawableTop * * @param drawableTop 资源 * @returnTop 返回 */ public CommonTextView setLeftDrawableTop(Drawable drawableTop) { if (drawableTop != null) { drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight()); } if (leftTextView == null) { initLeftText(); } leftTextView.setCompoundDrawables(null, drawableTop, null, null); return this; } /** * 设置左边view的drawableRight * * @param drawableRight 资源 * @return 返回 */ public CommonTextView setLeftDrawableRight(Drawable drawableRight) { if (drawableRight != null) { drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight()); } if (leftTextView == null) { initLeftText(); } leftTextView.setCompoundDrawables(null, null, drawableRight, null); return this; } /** * 设置左边view的drawableBottom * * @param drawableBottom 资源 * @return 返回 */ public CommonTextView setLeftDrawableBottom(Drawable drawableBottom) { if (drawableBottom != null) { drawableBottom.setBounds(0, 0, drawableBottom.getMinimumWidth(), drawableBottom.getMinimumHeight()); } if (leftTextView == null) { initLeftText(); } leftTextView.setCompoundDrawables(null, null, null, drawableBottom); return this; } /** * 设置中间view的drawableLeft * * @param drawableLeft 资源 * @return 返回 */ public CommonTextView setCenterDrawableLeft(Drawable drawableLeft) { if (drawableLeft != null) { drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight()); } if (centerTextView == null) { initCenterText(); } centerTextView.setCompoundDrawables(drawableLeft, null, null, null); return this; } /** * 设置中间view的drawableTop * * @param drawableTop 资源 * @returnTop 返回 */ public CommonTextView setCenterDrawableTop(Drawable drawableTop) { if (drawableTop != null) { drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight()); } if (centerTextView == null) { initCenterText(); } centerTextView.setCompoundDrawables(null, drawableTop, null, null); return this; } /** * 设置中间view的drawableRight * * @param drawableRight 资源 * @return 返回 */ public CommonTextView setCenterDrawableRight(Drawable drawableRight) { if (drawableRight != null) { drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight()); } if (centerTextView == null) { initCenterText(); } centerTextView.setCompoundDrawables(null, null, drawableRight, null); return this; } /** * 设置中间view的drawableBottom * * @param drawableBottom 资源 * @return 返回 */ public CommonTextView setCenterDrawableBottom(Drawable drawableBottom) { if (drawableBottom != null) { drawableBottom.setBounds(0, 0, drawableBottom.getMinimumWidth(), drawableBottom.getMinimumHeight()); } if (centerTextView == null) { initCenterText(); } centerTextView.setCompoundDrawables(null, null, null, drawableBottom); return this; } /** * 设置右边view的drawableLeft * * @param drawableLeft 资源 * @return 返回 */ public CommonTextView setRightDrawableLeft(Drawable drawableLeft) { if (drawableLeft != null) { drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight()); } if (rightTextView == null) { initRightText(); } rightTextView.setCompoundDrawables(drawableLeft, null, null, null); return this; } /** * 设置右边view的drawableTop * * @param drawableTop 资源 * @returnTop 返回 */ public CommonTextView setRightDrawableTop(Drawable drawableTop) { if (drawableTop != null) { drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight()); } if (rightTextView == null) { initRightText(); } rightTextView.setCompoundDrawables(null, drawableTop, null, null); return this; } /** * 设置右边view的drawableRight * * @param drawableRight 资源 * @return 返回 */ public CommonTextView setRightDrawableRight(Drawable drawableRight) { if (drawableRight != null) { drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight()); } if (rightTextView == null) { initRightText(); } rightTextView.setCompoundDrawables(null, null, drawableRight, null); return this; } /** * 设置右边view的drawableBottom * * @param drawableBottom 资源 * @return 返回 */ public CommonTextView setRightDrawableBottom(Drawable drawableBottom) { if (drawableBottom != null) { drawableBottom.setBounds(0, 0, drawableBottom.getMinimumWidth(), drawableBottom.getMinimumHeight()); } if (rightTextView == null) { initRightText(); } rightTextView.setCompoundDrawables(null, null, null, drawableBottom); return this; } /** * 获取左边imageView * * @return 返回imageView对象 */ public ImageView getLeftImageView() { if (leftImageView == null) { initLeftImageView(); } return leftImageView; } /** * 获取左边textView的值 * * @return 返回 */ public CharSequence getLeftTextString() { return leftTextView != null ? leftTextView.getText() : ""; } /** * 获取左上textView的值 * * @return 返回 */ public CharSequence getLeftTopTextString() { return leftTopTextView != null ? leftTopTextView.getText() : ""; } /** * 获取左下textView的值 * * @return 返回 */ public CharSequence getLeftBottomTextString() { return leftBottomTextView != null ? leftBottomTextView.getText() : ""; } /** * 获取中间textView的值 * * @return 返回 */ public CharSequence getCenterTextString() { return centerTextView != null ? centerTextView.getText() : ""; } /** * 获取中上textView的值 * * @return 返回 */ public CharSequence getCenterTopTextString() { return centerTopTextView != null ? centerTopTextView.getText() : ""; } /** * 获取中下textView的值 * * @return 返回 */ public CharSequence getCenterBottomTextString() { return centerBottomTextView != null ? centerBottomTextView.getText() : ""; } /** * 获取右边textView的值 * * @return 返回 */ public CharSequence getRightTextString() { return rightTextView != null ? rightTextView.getText() : ""; } /** * 获取右上textView的值 * * @return 返回 */ public CharSequence getRightTopTextString() { return rightTopTextView != null ? rightTopTextView.getText() : ""; } /** * 获取右下textView的值 * * @return 返回 */ public CharSequence getRightBottomTextString() { return rightBottomTextView != null ? rightBottomTextView.getText() : ""; } /** * 点击事件接口 */ public static class OnCommonTextViewClickListener { public void onCommonTextViewClick() { } public void onLeftViewClick() { } public void onCenterViewClick() { } public void onRightViewClick() { } } /** * 点击事件 * * @param listener listener对象 * @return 返回当前对象便于链式操作 */ public CommonTextView setOnCommonTextViewClickListener(OnCommonTextViewClickListener listener) { this.onCommonTextViewClickListener = listener; return this; } /** * 设置左边view可点击 * * @param isClickable boolean类型 * @return 返回 */ public CommonTextView setLeftViewIsClickable(boolean isClickable) { if (isClickable) { if (leftTextView != null) { leftTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onLeftViewClick(); } } }); } } return this; } /** * 设置中间view可点击 * * @param isClickable boolean类型 * @return 返回 */ public CommonTextView setCenterViewIsClickable(boolean isClickable) { if (isClickable) { if (centerTextView != null) { centerTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onCenterViewClick(); } } }); } } return this; } /** * 设置右边view可点击 * * @param isClickable boolean类型 * @return 返回 */ public CommonTextView setRightViewIsClickable(boolean isClickable) { if (isClickable) { if (rightTextView != null) { rightTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onCommonTextViewClickListener != null) { onCommonTextViewClickListener.onRightViewClick(); } } }); } } return this; } /** * 单位转换工具类 * * @param context 上下文对象 * @param dipValue 值 * @return 返回值 */ public int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 单位转换工具类 * * @param context 上下文对象 * @param pxValue 值 * @return 返回值 */ public int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 单位转换工具类 * * @param context 上下文对象 * @param spValue 值 * @return 返回值 */ public int sp2px(Context context, float spValue) { final float scale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * scale + 0.5f); } }
package com.loaderman.textviewdemo; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; /** * 万能的textView */ public class SuperTextView extends RelativeLayout { private Context mContext; private int defaultBgColor = 0xFFFFFFFF;//默认背景颜色 private int defaultLineColor = 0xFFE8E8E8;//线的默认颜色 //private int defaultLineColor = 0xFF535353;//文字默认颜色 private int defaultLinePadding = 0;//线的左右边距 private ImageView leftIconIV;//左边图标 private ImageView rightIconIV;//右边图标 private CheckBox rightCheckBox;//右边checkbox private Drawable rightCheckBoxBg;//checkBox的背景 private TextView leftTV;//左边textView private TextView centerTV;//中间textView private TextView rightTV;//右边textView private TextView leftTopTV;//左上的textView private TextView leftBottomTV;//左下的textView private TextView leftBottomTV2;//左下第二个textView private Drawable leftIconRes;//左边图标资源 private Drawable rightIconRes;//右边图标资源 private String leftTextString;//左边显示的文字 private String centerTextString;//中间显示的文字 private String rightTextString;//右边显示的文字 private String leftTopTextString;//左上显示的文字 private String leftBottomTextString;//左下显示的文字 private String leftBottomTextString2;//左下第二个显示的文字 private int defaultPadding = 0;//默认边距 private int centerSpaceHeight;//中间空间的高度 private int bothLineWidth; private int topLineWidth; private int bottomLineWidth; private int lineColor = 0xFFE8E8E8;//线的默认颜色 private int topLineMargin;//上边线的左右边距 private int topLineMarginLeft;//上边线的左边距 private int topLineMarginRight;//上边线的右边距 private int bottomLineMargin;//下边线的左右边距 private int bottomLineMarginLeft;//下边线的左边距 private int bottomLineMarginRight;//下边线的右边距 private int bothLineMargin;//两条线的左右边距 private int bothLineMarginLeft;//两条线的左边距 private int bothLineMarginRight;//两条线的右边距 private int leftIconMarginLeft;//左边图标的左边距 private int leftTVMarginLeft;//左边文字的左边距 private int leftIconWidth;//左边图标的宽 private int leftIconHeight;//左边图标的高 private int rightIconWidth;//右边图标的宽 private int rightIconHeight;//右边图标的高 private int leftTopMarginLeft;//左上文字的左边距 private int leftBottomMarginLeft;//左下文字的左边距 private int leftBottomMarginLeft2;//左下第二个文字的左边距 private int rightTVMarginRight;//右边文字的右边距 private int rightIconMarginRight;//右边图标的右边距 private int rightCheckBoxMarginRight;//右边checkBox的右边距 private boolean showCheckBox;//是否显示右边选择框 private boolean isChecked;//是否默认选中 private int defaultSize = 0;//默认字体大小 private int leftTVSize;//左边文字字体大小 private int leftTopTVSize;//左上文字字体大小 private int leftBottomTVSize;//左下文字字体大小 private int leftBottomTVSize2;//左下第二个文字字体大小 private int rightTVSize;//右边文字字体大小 private int centerTVSize;//中间文字字体大小 private int defaultColor = 0xFF373737;//文字默认颜色 private int backgroundColor;//背景颜色 private int leftTVColor;//左边文字颜色 private int leftTopTVColor;//左上文字颜色 private int leftBottomTVColor;//左下文字颜色 private int leftBottomTVColor2;//左下第二个文字颜色 private int rightTVColor;//右边文字颜色 private int centerTVColor;//中间文字颜色 private boolean isSingLines = true;//是否单行显示 默认单行 private int maxLines = 1;//最多几行 默认显示一行 private int maxEms = 10;//最多几个字 默认显示10个汉子 private static final int NONE = 0; private static final int TOP = 1; private static final int BOTTOM = 2; private static final int BOTH = 3; private static final int DEFAULT = BOTTOM; public static final int leftTextViewId = 0; public static final int leftTopTextViewId = 1; public static final int leftBottomTextViewId = 2; public static final int leftBottomTextViewId2 = 3; public static final int rightTextViewId = 4; public static final int centerTextViewId = 5; public static final int leftImageViewId = 6; public static final int rightImageViewId = 7; private boolean useRipple; private int lineType; private LayoutParams centerBaseLineParams, topLineParams, bottomLineParams, leftImgParams, leftTextParams, centerTextParams, leftTopTextParams, leftBottomParams, leftBottomParams2, rightTextParams, rightImgParams, rightCheckBoxParams; private OnSuperTextViewClickListener onSuperTextViewClickListener; private Drawable rightTextStringRightIconRes; private int rightTextStringRightIconPadding; private boolean mLeftTopViewIsClickable = false; private boolean mLeftBottomViewIsClickable = false; private boolean mLeftBottomView2IsClickable = false; private Drawable mBackground_drawable; public SuperTextView(Context context) { super(context); } public SuperTextView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; // defaultLinePadding = dip2px(context, 16); defaultPadding = dip2px(context, 16); defaultSize = sp2px(context, 14); centerSpaceHeight = dip2px(context, 10); getAttr(attrs); initLayout(); } /** * 获取自定义属性值 * * @param attrs */ private void getAttr(AttributeSet attrs) { TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.SuperTextView); ////////设置文字或者图片资源//////// leftIconRes = typedArray.getDrawable(R.styleable.SuperTextView_sLeftIconRes); rightIconRes = typedArray.getDrawable(R.styleable.SuperTextView_sRightIconRes); rightCheckBoxBg = typedArray.getDrawable(R.styleable.SuperTextView_sRightCheckBoxRes); leftTextString = typedArray.getString(R.styleable.SuperTextView_sLeftTextString); centerTextString = typedArray.getString(R.styleable.SuperTextView_sCenterTextString); rightTextString = typedArray.getString(R.styleable.SuperTextView_sRightTextString); rightTextStringRightIconRes = typedArray.getDrawable(R.styleable.SuperTextView_sRightTextStringRightIconRes); rightTextStringRightIconPadding = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightTextStringRightIconResPadding, dip2px(mContext, 5)); leftTopTextString = typedArray.getString(R.styleable.SuperTextView_sLeftTopTextString); leftBottomTextString = typedArray.getString(R.styleable.SuperTextView_sLeftBottomTextString); leftBottomTextString2 = typedArray.getString(R.styleable.SuperTextView_sLeftBottomTextString2); showCheckBox = typedArray.getBoolean(R.styleable.SuperTextView_sRightCheckBoxShow, false); isChecked = typedArray.getBoolean(R.styleable.SuperTextView_sIsChecked, false); useRipple = typedArray.getBoolean(R.styleable.SuperTextView_sUseRipple, false); lineType = typedArray.getInt(R.styleable.SuperTextView_sLineShow, DEFAULT); /////////设置view的边距//////// centerSpaceHeight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sCenterSpaceHeight, centerSpaceHeight); bothLineWidth = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBothLineWidth, dip2px(mContext, 0.5f)); topLineWidth = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sTopLineWidth, dip2px(mContext, 0.5f)); bottomLineWidth = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBottomLineWidth, dip2px(mContext, 0.5f)); lineColor = typedArray.getColor(R.styleable.SuperTextView_sLineColor, lineColor); topLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sTopLineMargin, defaultLinePadding); topLineMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sTopLineMarginLeft, defaultLinePadding); topLineMarginRight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sTopLineMarginRight, defaultLinePadding); bottomLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBottomLineMargin, defaultLinePadding); bottomLineMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBottomLineMarginLeft, defaultLinePadding); bottomLineMarginRight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBottomLineMarginRight, defaultLinePadding); bothLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBothLineMargin, defaultLinePadding); bothLineMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBothLineMarginLeft, defaultLinePadding); bothLineMarginRight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBothLineMarginRight, defaultLinePadding); leftIconMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftIconMarginLeft, defaultPadding); leftTVMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftTextMarginLeft, defaultPadding); leftTopMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftTopTextMarginLeft, defaultPadding); leftBottomMarginLeft = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftBottomTextMarginLeft, defaultPadding); leftBottomMarginLeft2 = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftBottomTextMarginLeft2, defaultPadding); rightTVMarginRight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightTextMarginRight, defaultPadding); rightIconMarginRight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightIconMarginRight, defaultPadding); rightCheckBoxMarginRight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightCheckBoxMarginRight, defaultPadding); //////设置字体颜色//////// backgroundColor = typedArray.getColor(R.styleable.SuperTextView_sBackgroundColor, defaultBgColor); leftTVColor = typedArray.getColor(R.styleable.SuperTextView_sLeftTextColor, defaultColor); leftTopTVColor = typedArray.getColor(R.styleable.SuperTextView_sLeftTopTextColor, defaultColor); leftBottomTVColor = typedArray.getColor(R.styleable.SuperTextView_sLeftBottomTextColor, defaultColor); leftBottomTVColor2 = typedArray.getColor(R.styleable.SuperTextView_sLeftBottomTextColor2, defaultColor); rightTVColor = typedArray.getColor(R.styleable.SuperTextView_sRightTextColor, defaultColor); centerTVColor = typedArray.getColor(R.styleable.SuperTextView_sCenterTextColor, defaultColor); //////设置字体大小//////// leftTVSize = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftTextSize, defaultSize); leftTopTVSize = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftTopTextSize, defaultSize); leftBottomTVSize = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftBottomTextSize, defaultSize); leftBottomTVSize2 = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftBottomTextSize2, defaultSize); rightTVSize = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightTextSize, defaultSize); centerTVSize = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sCenterTextSize, defaultSize); ///////设置textView的属性///////////SuperTextViewxEms isSingLines = typedArray.getBoolean(R.styleable.SuperTextView_sIsSingLines, isSingLines); maxLines = typedArray.getInt(R.styleable.SuperTextView_sMaxLines, maxLines); maxEms = typedArray.getInt(R.styleable.SuperTextView_sMaxEms, maxEms); leftIconWidth = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftIconWidth, 0); leftIconHeight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sLeftIconHeight, 0); rightIconWidth = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightIconWidth, 0); rightIconHeight = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sRightIconHeight, 0); mLeftTopViewIsClickable = typedArray.getBoolean(R.styleable.SuperTextView_sLeftTopViewIsClickable, false); mLeftBottomViewIsClickable = typedArray.getBoolean(R.styleable.SuperTextView_sLeftBottomViewIsClickable, false); mLeftBottomView2IsClickable = typedArray.getBoolean(R.styleable.SuperTextView_sLeftBottomView2IsClickable, false); mBackground_drawable = typedArray.getDrawable(R.styleable.SuperTextView_sBackgroundDrawableRes); typedArray.recycle(); } /** * 初始化布局 */ private void initLayout() { initSuperTextView(); initCenterBaseLine(); if (leftIconRes != null) { initLeftIcon(); } if (leftTopTextString != null) { initLeftTopText(); } if (leftBottomTextString != null) { initLeftBottomText(); } if (leftBottomTextString2 != null) { initLeftBottomText2(); } if (leftTextString != null) { initLeftText(); } if (centerTextString != null) { initCenterText(); } if (rightIconRes != null) { initRightIcon(); } if (rightTextString != null || rightTextStringRightIconRes != null) { initRightText(); } if (showCheckBox) { initRightCheckBox(); } switch (lineType) { case NONE: break; case TOP: setTopLineMargin(); break; case BOTTOM: setBottomLineMargin(); break; case BOTH: setTopLineMargin(); setBottomLineMargin(); break; } } /** * 设置顶部分割线的左右边距 */ private void setTopLineMargin() { if (topLineMargin != 0) { initTopLine(topLineMargin, topLineMargin, topLineWidth); } else if (bothLineMarginLeft != 0 | bothLineMarginRight != 0) { initTopLine(bothLineMarginLeft, bothLineMarginRight, topLineWidth); } else { initTopLine(topLineMarginLeft, topLineMarginRight, topLineWidth); } } /** * 设置底部分割线的左右边距 */ private void setBottomLineMargin() { if (bottomLineMargin != 0) { initBottomLine(bottomLineMargin, bottomLineMargin, bottomLineWidth); } else if (bothLineMarginLeft != 0 | bothLineMarginRight != 0) { initBottomLine(bothLineMarginLeft, bothLineMarginRight, topLineWidth); } else { initBottomLine(bottomLineMarginLeft, bottomLineMarginRight, topLineWidth); } } /** * 初始化上边的线 */ private void initTopLine(int lineMarginLeft, int lineMarginRight, int lineWidth) { View topLine = new View(mContext); topLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, lineWidth); topLineParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, TRUE); topLineParams.setMargins(lineMarginLeft, 0, lineMarginRight, 0); topLine.setLayoutParams(topLineParams); topLine.setBackgroundColor(lineColor); addView(topLine); } /** * 初始化下边的线 */ private void initBottomLine(int lineMarginLeft, int lineMarginRight, int lineWidth) { View bottomLine = new View(mContext); bottomLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, lineWidth); bottomLineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, TRUE); bottomLineParams.setMargins(lineMarginLeft, 0, lineMarginRight, 0); bottomLine.setLayoutParams(bottomLineParams); bottomLine.setBackgroundColor(lineColor); addView(bottomLine); } /** * 初始化SuperTextView */ private void initSuperTextView() { this.setBackgroundColor(backgroundColor); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onSuperTextViewClick(); } } }); if (useRipple) { this.setBackgroundResource(R.drawable.selector_white); } if (mBackground_drawable != null) { this.setBackgroundDrawable(mBackground_drawable); } } /** * 为了设置上下两排文字居中对齐显示而需要设置的基准线 */ private void initCenterBaseLine() { View view = new View(mContext); centerBaseLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, centerSpaceHeight); centerBaseLineParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); view.setId(R.id.sCenterBaseLineId); view.setLayoutParams(centerBaseLineParams); addView(view); } /** * 初始化左边图标 */ private void initLeftIcon() { leftIconIV = new ImageView(mContext); leftImgParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftImgParams.addRule(ALIGN_PARENT_LEFT, TRUE); leftImgParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); if (leftIconHeight != 0 && leftIconWidth != 0) { leftImgParams.width = leftIconWidth; leftImgParams.height = leftIconHeight; } setMargin(leftImgParams, leftIconMarginLeft, 0, 0, 0); leftIconIV.setScaleType(ImageView.ScaleType.FIT_CENTER); leftIconIV.setId(R.id.sLeftIconId); leftIconIV.setLayoutParams(leftImgParams); if (leftIconRes != null) { leftIconIV.setImageDrawable(leftIconRes); } addView(leftIconIV); } /** * 初始化左边文字 */ private void initLeftText() { leftTV = new TextView(mContext); leftTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftTextParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); leftTextParams.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftIconId); setMargin(leftTextParams, leftTVMarginLeft, 0, dip2px(mContext, 10), 0); leftTV.setId(R.id.sLeftTextId); leftTV.setLayoutParams(leftTextParams); leftTV.setText(leftTextString); setTextViewParams(leftTV, isSingLines, maxLines, maxEms); setTextColor(leftTV, leftTVColor); setTextSize(leftTV, leftTVSize); addView(leftTV); } /** * 设置通用的textView显示效果属性 * * @param textView view * @param isSingLines 是否单行显示 * @param maxLines 显示最大行 * @param maxEms 最多显示多少个字 */ private void setTextViewParams(TextView textView, boolean isSingLines, int maxLines, int maxEms) { textView.setSingleLine(isSingLines); textView.setMaxLines(maxLines); textView.setMaxEms(maxEms); textView.setEllipsize(TextUtils.TruncateAt.END); } /** * 初始化左上文字 */ private void initLeftTopText() { leftTopTV = new TextView(mContext); leftTopTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftTopTextParams.addRule(RelativeLayout.ABOVE, R.id.sCenterBaseLineId); leftTopTextParams.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftIconId); setMargin(leftTopTextParams, leftTopMarginLeft, 0, 0, 0); leftTopTV.setId(R.id.sLeftTopTextId); leftTopTV.setLayoutParams(leftTopTextParams); leftTopTV.setText(leftTopTextString); setTextColor(leftTopTV, leftTopTVColor); setTextSize(leftTopTV, leftTopTVSize); if (mLeftTopViewIsClickable) { leftTopTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onLeftTopClick(); } } }); } setTextViewParams(leftTopTV, isSingLines, maxLines, maxEms); addView(leftTopTV); } /** * 初始化左下文字 */ private void initLeftBottomText() { leftBottomTV = new TextView(mContext); leftBottomParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftBottomParams.addRule(RelativeLayout.BELOW, R.id.sCenterBaseLineId); leftBottomParams.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftIconId); setMargin(leftBottomParams, leftBottomMarginLeft, 0, 0, 0); leftBottomTV.setId(R.id.sLeftBottomTextId); leftBottomTV.setLayoutParams(leftBottomParams); leftBottomTV.setText(leftBottomTextString); setTextColor(leftBottomTV, leftBottomTVColor); setTextSize(leftBottomTV, leftBottomTVSize); if (mLeftBottomViewIsClickable) { leftBottomTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onLeftBottomClick(); } } }); } setTextViewParams(leftBottomTV, isSingLines, maxLines, maxEms); addView(leftBottomTV); } /** * 初始化左下第二个文字 */ private void initLeftBottomText2() { leftBottomTV2 = new TextView(mContext); leftBottomParams2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftBottomParams2.addRule(RelativeLayout.BELOW, R.id.sCenterBaseLineId); leftBottomParams2.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftBottomTextId); setMargin(leftBottomParams2, leftBottomMarginLeft2, 0, 0, 0); leftBottomTV2.setId(R.id.sLeftBottomTextId2); leftBottomTV2.setLayoutParams(leftBottomParams2); leftBottomTV2.setText(leftBottomTextString2); setTextColor(leftBottomTV2, leftBottomTVColor2); setTextSize(leftBottomTV2, leftBottomTVSize2); if (mLeftBottomView2IsClickable) { leftBottomTV2.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onLeftBottomClick2(); } } }); } setTextViewParams(leftBottomTV2, isSingLines, maxLines, maxEms); addView(leftBottomTV2); } /** * 初始化中间文字 */ private void initCenterText() { centerTV = new TextView(mContext); centerTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); centerTextParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); centerTV.setId(R.id.sCenterTextId); centerTV.setLayoutParams(centerTextParams); centerTV.setText(centerTextString); setTextColor(centerTV, centerTVColor); setTextSize(centerTV, centerTVSize); setTextViewParams(centerTV, isSingLines, maxLines, maxEms); addView(centerTV); } /** * 初始化右边文字 */ private void initRightText() { rightTV = new TextView(mContext); rightTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightTextParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); rightTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); rightTextParams.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftTextId); rightTextParams.addRule(RelativeLayout.LEFT_OF, R.id.sRightIconId); setMargin(rightTextParams, 0, 0, rightTVMarginRight, 0); rightTV.setId(R.id.sRightTextId); rightTV.setLayoutParams(rightTextParams); rightTV.setText(rightTextString); setTextColor(rightTV, rightTVColor); setTextSize(rightTV, rightTVSize); setTextViewRightDrawble(rightTV, rightTextStringRightIconRes, rightTextStringRightIconPadding); rightTV.setGravity(Gravity.RIGHT); setTextViewParams(rightTV, isSingLines, maxLines, maxEms); addView(rightTV); } /** * 初始化右边图标 */ private void initRightIcon() { rightIconIV = new ImageView(mContext); rightImgParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightImgParams.addRule(ALIGN_PARENT_RIGHT, TRUE); rightImgParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); if (rightIconHeight != 0 && rightIconWidth != 0) { rightImgParams.width = rightIconWidth; rightImgParams.height = rightIconHeight; } setMargin(rightImgParams, 0, 0, rightIconMarginRight, 0); rightIconIV.setScaleType(ImageView.ScaleType.FIT_CENTER); rightIconIV.setId(R.id.sRightIconId); rightIconIV.setLayoutParams(rightImgParams); if (rightIconRes != null) { rightIconIV.setImageDrawable(rightIconRes); } addView(rightIconIV); } /** * 初始化右边选择框 */ private void initRightCheckBox() { rightCheckBox = new CheckBox(mContext); rightCheckBoxParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightCheckBoxParams.addRule(ALIGN_PARENT_RIGHT, TRUE); rightCheckBoxParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); setMargin(rightCheckBoxParams, 0, 0, rightCheckBoxMarginRight, 0); rightCheckBox.setLayoutParams(rightCheckBoxParams); if (rightCheckBoxBg != null) { rightCheckBox.setGravity(CENTER_IN_PARENT); rightCheckBox.setButtonDrawable(rightCheckBoxBg); } rightCheckBox.setChecked(isChecked); addView(rightCheckBox); } private void setMargin(LayoutParams params, int left, int top, int right, int bottom) { params.setMargins(left, top, right, bottom); } /** * 设置view的边距 * * @param view view对象 * @param left 左边边距 * @param top 上边边距 * @param right 右边边距 * @param bottom 下边边距 */ private void setPadding(View view, int left, int top, int right, int bottom) { view.setPadding(left, top, right, bottom); } /** * 设置文字的字体大小 * * @param textView textView对象 * @param size 文字大小 */ private void setTextSize(TextView textView, int size) { textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); } /** * 设置文字的颜色 * * @param textView textView对象 * @param color 文字颜色 */ private void setTextColor(TextView textView, int color) { textView.setTextColor(color); } //////////对外公布的方法/////////////// /** * 设置左边图标 * * @param leftIcon 左边图标 * @return 返回对象 */ public SuperTextView setLeftIcon(Drawable leftIcon) { leftIconRes = leftIcon; if (leftIconIV == null) { initLeftIcon(); } else { leftIconIV.setImageDrawable(leftIcon); } return this; } /** * 设置右边图标 * * @param rightIcon 右边图标 * @return 返回对象 */ public SuperTextView setRightIcon(Drawable rightIcon) { rightIconRes = rightIcon; if (rightIconIV == null) { initRightIcon(); } else { rightIconIV.setImageDrawable(rightIcon); } return this; } /** * 设置左边显示文字 * * @param leftString 左边文字 * @return 返回对象 */ public SuperTextView setLeftString(String leftString) { leftTextString = leftString; if (leftTV == null) { initLeftText(); } else { leftTV.setText(leftString); } return this; } /** * 设置左上显示的文字 * * @param leftTopString 左上文字 * @return 返回对象 */ public SuperTextView setLeftTopString(String leftTopString) { leftTopTextString = leftTopString; if (leftTopTV == null) { initLeftTopText(); } else { leftTopTV.setText(leftTopString); } return this; } /** * 设置左下显示的文字 * * @param leftBottomString 左下第一个文字 * @return 返回对象 */ public SuperTextView setLeftBottomString(String leftBottomString) { leftBottomTextString = leftBottomString; if (leftBottomTV == null) { initLeftBottomText(); } else { leftBottomTV.setText(leftBottomString); } return this; } /** * 设置左下第二个显示的文字 * * @param leftBottomString2 左下第二个文字 * @return 返回对象 */ public SuperTextView setLeftBottomString2(String leftBottomString2) { leftBottomTextString2 = leftBottomString2; if (leftBottomTV2 == null) { initLeftBottomText2(); } else { leftBottomTV2.setText(leftBottomString2); } return this; } /** * 设置右边显示的文字 * * @param rightString 右边文字 * @return 返回对象 */ public SuperTextView setRightString(String rightString) { rightTextString = rightString; if (rightTV == null) { initRightText(); } else { rightTV.setText(rightString); } return this; } /** * 设置右边显示的文字和图片 * * @param rightString 右边文字 * @param drawable drawable * @param drawablePadding drawablePadding * @return */ public SuperTextView setRightString(String rightString, Drawable drawable, int drawablePadding) { rightTextString = rightString; rightTextStringRightIconRes = drawable; rightTextStringRightIconPadding = drawablePadding; if (rightTV == null) { initRightText(); } else { rightTV.setText(rightString); } return this; } /** * 设备中间文字 * * @param centerString 中间文字 * @return 返回对象 */ public SuperTextView setCenterString(String centerString) { centerTextString = centerString; if (centerTV == null) { initCenterText(); } else { centerTV.setText(centerString); } return this; } /** * @param checked 是否选中 * @return 返回值 */ public SuperTextView setCbChecked(boolean checked) { isChecked = checked; if (rightCheckBox == null) { initRightCheckBox(); } else { rightCheckBox.setChecked(checked); } return this; } /** * 设置checkbox的背景图 * * @param drawable drawable对象 * @return 返回对象 */ public SuperTextView setCbBackground(Drawable drawable) { rightCheckBoxBg = drawable; if (rightCheckBox == null) { initRightCheckBox(); } else { rightCheckBox.setBackgroundDrawable(drawable); } return this; } /** * 获取checkbox状态 * * @return 返回选择框当前选中状态 */ public boolean getCbisChecked() { boolean isChecked = false; if (rightCheckBox != null) { isChecked = rightCheckBox.isChecked(); } return isChecked; } /** * 设置左边文字的颜色 * * @param textColor 文字颜色值 * @return 返回对象 */ public SuperTextView setLeftTVColor(int textColor) { leftTVColor = textColor; if (leftTV == null) { initLeftText(); } else { leftTV.setTextColor(textColor); } return this; } /** * 设置右边文字的颜色 * * @param textColor 文字颜色值 * @return 返回对象 */ public SuperTextView setRightTVColor(int textColor) { rightTVColor = textColor; if (rightTV == null) { initRightText(); } else { rightTV.setTextColor(textColor); } return this; } /** * 设置左上边文字的颜色 * * @param textColor 文字颜色值 * @return 返回对象 */ public SuperTextView setLeftTopTVColor(int textColor) { leftTopTVColor = textColor; if (leftTopTV == null) { initLeftTopText(); } else { leftTopTV.setTextColor(textColor); } return this; } /** * 设置左下边文字的颜色 * * @param textColor 文字颜色值 * @return 返回对象 */ public SuperTextView setLeftBottomTVColor(int textColor) { leftBottomTVColor = textColor; if (leftBottomTV == null) { initLeftBottomText(); } else { leftBottomTV.setTextColor(textColor); } return this; } /** * 设置左下第二个文字的颜色 * * @param textColor 文字颜色值 * @return 返回对象 */ public SuperTextView setLeftBottomTVColor2(int textColor) { leftBottomTVColor2 = textColor; if (leftBottomTV2 == null) { initLeftBottomText2(); } else { leftBottomTV2.setTextColor(textColor); } return this; } //////////设置View的点击事件///////////////// /** * 点击事件 * * @param listener listener对象 * @return 返回对象 */ public SuperTextView setOnSuperTextViewClickListener(OnSuperTextViewClickListener listener) { onSuperTextViewClickListener = listener; return this; } /** * 点击事件接口 */ public static class OnSuperTextViewClickListener { public void onSuperTextViewClick() { } public void onLeftTopClick() { } public void onLeftBottomClick() { } public void onLeftBottomClick2() { } } /** * 获取控件ID便于根据ID设置值 * * @param viewName 需要的textViewName * @return 返回ID */ public int getViewId(int viewName) { int viewId = 0; switch (viewName) { case leftTextViewId: if (leftTV == null) { initLeftText(); } viewId = R.id.sLeftTextId; break; case leftTopTextViewId: if (leftTopTV == null) { initLeftTopText(); } viewId = R.id.sLeftTopTextId; break; case leftBottomTextViewId: if (leftBottomTV == null) { initLeftBottomText(); } viewId = R.id.sLeftBottomTextId; break; case leftBottomTextViewId2: if (leftBottomTV2 == null) { initLeftBottomText2(); } viewId = R.id.sLeftBottomTextId2; break; case centerTextViewId: if (centerTV == null) { initCenterText(); } viewId = R.id.sCenterTextId; break; case rightTextViewId: if (rightTV == null) { initRightText(); } viewId = R.id.sRightTextId; break; case leftImageViewId: if (leftIconIV == null) { initLeftIcon(); } viewId = R.id.sLeftIconId; break; case rightImageViewId: if (rightIconIV == null) { initRightIcon(); } viewId = R.id.sRightIconId; break; } return viewId; } /** * 获取view对象 * * @param viewName 传入viewName * @return 返回view */ public View getView(int viewName) { View view = null; switch (viewName) { case leftImageViewId: if (leftIconIV == null) { initLeftIcon(); } view = leftIconIV; break; case rightImageViewId: if (rightIconIV == null) { initRightIcon(); } view = rightIconIV; break; } return view; } public int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } public int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } public int sp2px(Context context, float spValue) { final float scale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * scale + 0.5f); } public static void setTextViewRightDrawble(TextView textView, Drawable drawable, int drawablePadding) { if (drawable != null && textView != null) { drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textView.setCompoundDrawables(null, null, drawable, null); textView.setCompoundDrawablePadding(drawablePadding); } } /** * 设置左上view可点击 * * @param isClickable boolean类型 * @return 返回 */ public SuperTextView setLeftTopViewIsClickable(boolean isClickable) { if (isClickable) { if (leftTopTV != null) { leftTopTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onLeftTopClick(); } } }); } } return this; } /** * 设置左下第一个view可点击 * * @param isClickable boolean类型 * @return 返回 */ public SuperTextView setLeftBottomViewIsClickable(boolean isClickable) { if (isClickable) { if (leftBottomTV != null) { leftBottomTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onLeftBottomClick(); } } }); } } return this; } /** * 设置左下第二个view可点击 * * @param isClickable boolean类型 * @return 返回 */ public SuperTextView setLeftBottomView2IsClickable(boolean isClickable) { if (isClickable) { if (leftBottomTV2 != null) { leftBottomTV2.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (onSuperTextViewClickListener != null) { onSuperTextViewClickListener.onLeftBottomClick2(); } } }); } } return this; } }
selector_while.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/common_pressed" android:state_pressed="true" /> <item android:drawable="@color/white" /> </selector>
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="BaseTextView"> <attr name="sDrawableLeft" format="reference" /> <attr name="sDrawableTop" format="reference" /> <attr name="sDrawableRight" format="reference" /> <attr name="sDrawableBottom" format="reference" /> <attr name="sTextString" format="string" /> <attr name="sTextSize" format="dimension" /> <attr name="sTextColor" format="color" /> <attr name="sDrawablePadding" format="dimension" /> <attr name="sPadding" format="dimension" /> <attr name="sPaddingLeft" format="dimension" /> <attr name="sPaddingTop" format="dimension" /> <attr name="sPaddingBottom" format="dimension" /> <attr name="sPaddingRight" format="dimension" /> </declare-styleable> <declare-styleable name="SuperTextView"> <attr name="sLeftIconRes" format="reference" /> <attr name="sRightIconRes" format="reference" /> <attr name="sLeftIconWidth" format="dimension" /> <attr name="sLeftIconHeight" format="dimension" /> <attr name="sRightIconWidth" format="dimension" /> <attr name="sRightIconHeight" format="dimension" /> <attr name="sRightCheckBoxRes" format="reference" /> <attr name="sLeftTextString" format="string" /> <attr name="sCenterTextString" format="string" /> <attr name="sRightTextString" format="string" /> <attr name="sLeftTopTextString" format="string" /> <attr name="sLeftBottomTextString" format="string" /> <attr name="sLeftBottomTextString2" format="string" /> <attr name="sTopLineMargin" format="dimension" /> <attr name="sTopLineMarginLeft" format="dimension" /> <attr name="sTopLineMarginRight" format="dimension" /> <attr name="sBottomLineMargin" format="dimension" /> <attr name="sBottomLineMarginLeft" format="dimension" /> <attr name="sBottomLineMarginRight" format="dimension" /> <attr name="sBothLineMargin" format="dimension" /> <attr name="sBothLineMarginLeft" format="dimension" /> <attr name="sBothLineMarginRight" format="dimension" /> <attr name="sCenterSpaceHeight" format="dimension" /> <attr name="sLeftIconMarginLeft" format="dimension" /> <attr name="sLeftTextMarginLeft" format="dimension" /> <attr name="sLeftTopTextMarginLeft" format="dimension" /> <attr name="sLeftBottomTextMarginLeft" format="dimension" /> <attr name="sLeftBottomTextMarginLeft2" format="dimension" /> <attr name="sRightTextStringRightIconRes" format="reference" /> <attr name="sRightTextStringRightIconResPadding" format="dimension" /> <attr name="sRightIconMarginRight" format="dimension" /> <attr name="sRightTextMarginRight" format="dimension" /> <attr name="sRightCheckBoxMarginRight" format="dimension" /> <attr name="sRightCheckBoxShow" format="boolean" /> <attr name="sIsChecked" format="boolean" /> <attr name="sUseRipple" format="boolean" /> <attr name="sLeftTextSize" format="dimension" /> <attr name="sLeftTopTextSize" format="dimension" /> <attr name="sLeftBottomTextSize" format="dimension" /> <attr name="sLeftBottomTextSize2" format="dimension" /> <attr name="sRightTextSize" format="dimension" /> <attr name="sCenterTextSize" format="dimension" /> <attr name="sBackgroundColor" format="color" /> <attr name="sLeftTextColor" format="color" /> <attr name="sLeftTopTextColor" format="color" /> <attr name="sLeftBottomTextColor" format="color" /> <attr name="sLeftBottomTextColor2" format="color" /> <attr name="sRightTextColor" format="color" /> <attr name="sCenterTextColor" format="color" /> <attr name="sIsSingLines" format="boolean" /> <attr name="sMaxLines" format="integer" /> <attr name="sMaxEms" format="integer" /> <attr name="sLineShow" format="enum"> <enum name="none" value="0" /> <enum name="top" value="1" /> <enum name="bottom" value="2" /> <enum name="both" value="3" /> </attr> <attr name="sBothLineWidth" format="dimension" /> <attr name="sTopLineWidth" format="dimension" /> <attr name="sBottomLineWidth" format="dimension" /> <attr name="sLineColor" format="color" /> <attr name="sLeftTopViewIsClickable" format="boolean" /> <attr name="sLeftBottomViewIsClickable" format="boolean" /> <attr name="sLeftBottomView2IsClickable" format="boolean" /> <attr name="sBackgroundDrawableRes" format="reference" /> </declare-styleable> <declare-styleable name="CommonTextView"> <attr name="cLeftIconResForDrawableLeft" format="reference" /> <attr name="cLeftIconResForDrawableTop" format="reference" /> <attr name="cLeftIconResForDrawableRight" format="reference" /> <attr name="cLeftIconResForDrawableBottom" format="reference" /> <attr name="cCenterIconResForDrawableLeft" format="reference" /> <attr name="cCenterIconResForDrawableTop" format="reference" /> <attr name="cCenterIconResForDrawableRight" format="reference" /> <attr name="cCenterIconResForDrawableBottom" format="reference" /> <attr name="cRightIconResForDrawableLeft" format="reference" /> <attr name="cRightIconResForDrawableTop" format="reference" /> <attr name="cRightIconResForDrawableRight" format="reference" /> <attr name="cRightIconResForDrawableBottom" format="reference" /> <attr name="cLeftImageViewDrawableRes" format="reference" /> <!--<attr name="cRightImageViewDrawableRes" format="reference" />--> <attr name="cLeftTextString" format="string" /> <attr name="cLeftTopTextString" format="string" /> <attr name="cLeftBottomTextString" format="string" /> <attr name="cCenterTextString" format="string" /> <attr name="cCenterTopTextString" format="string" /> <attr name="cCenterBottomTextString" format="string" /> <attr name="cRightTextString" format="string" /> <attr name="cRightTopTextString" format="string" /> <attr name="cRightBottomTextString" format="string" /> <attr name="cLeftTextSize" format="dimension" /> <attr name="cLeftTopTextSize" format="dimension" /> <attr name="cLeftBottomTextSize" format="dimension" /> <attr name="cCenterTextSize" format="dimension" /> <attr name="cCenterTopTextSize" format="dimension" /> <attr name="cCenterBottomTextSize" format="dimension" /> <attr name="cRightTextSize" format="dimension" /> <attr name="cRightTopTextSize" format="dimension" /> <attr name="cRightBottomTextSize" format="dimension" /> <attr name="cLeftTextColor" format="color" /> <attr name="cLeftTopTextColor" format="color" /> <attr name="cLeftBottomTextColor" format="color" /> <attr name="cCenterTextColor" format="color" /> <attr name="cCenterTopTextColor" format="color" /> <attr name="cCenterBottomTextColor" format="color" /> <attr name="cRightTextColor" format="color" /> <attr name="cRightTopTextColor" format="color" /> <attr name="cRightBottomTextColor" format="color" /> <attr name="cLeftIconDrawablePadding" format="dimension" /> <attr name="cCenterIconDrawablePadding" format="dimension" /> <attr name="cRightIconDrawablePadding" format="dimension" /> <attr name="cLeftViewPaddingLeft" format="dimension" /> <attr name="cLeftViewPaddingRight" format="dimension" /> <attr name="cCenterViewPaddingLeft" format="dimension" /> <attr name="cCenterViewPaddingRight" format="dimension" /> <attr name="cRightViewPaddingLeft" format="dimension" /> <attr name="cRightViewPaddingRight" format="dimension" /> <attr name="cTopDividerLineMarginLR" format="dimension" /> <attr name="cTopDividerLineMarginLeft" format="dimension" /> <attr name="cTopDividerLineMarginRight" format="dimension" /> <attr name="cBottomDividerLineMarginLR" format="dimension" /> <attr name="cBottomDividerLineMarginLeft" format="dimension" /> <attr name="cBottomDividerLineMarginRight" format="dimension" /> <attr name="cBothDividerLineMarginLeft" format="dimension" /> <attr name="cBothDividerLineMarginRight" format="dimension" /> <attr name="cLeftTextViewLineSpacingExtra" format="dimension" /> <attr name="cCenterTextViewLineSpacingExtra" format="dimension" /> <attr name="cRightTextViewLineSpacingExtra" format="dimension" /> <attr name="cCenterSpaceHeight" format="dimension" /> <attr name="cLeftImageViewMarginLeft" format="dimension" /> <!--<attr name="cRightImageViewMarginRight" format="dimension" />--> <attr name="cDividerLineColor" format="color" /> <attr name="cDividerLineHeight" format="dimension" /> <attr name="cShowDividerLineType" format="enum"> <enum name="none" value="0" /> <enum name="top" value="1" /> <enum name="bottom" value="2" /> <enum name="both" value="3" /> </attr> <attr name="cUseRipple" format="boolean" /> <attr name="cBackgroundColor" format="color" /> <attr name="cSetSingleLine" format="boolean" /> <attr name="cSetMaxEms" format="integer" /> <attr name="cSetLines" format="integer" /> <attr name="cLeftTextViewGravity" format="enum"> <enum name="left_center" value="0" /> <enum name="center" value="1" /> <enum name="right_center" value="2" /> </attr> <attr name="cCenterTextViewGravity" format="enum"> <enum name="left_center" value="0" /> <enum name="center" value="1" /> <enum name="right_center" value="2" /> </attr> <attr name="cRightTextViewGravity" format="enum"> <enum name="left_center" value="0" /> <enum name="center" value="1" /> <enum name="right_center" value="2" /> </attr> <attr name="cLeftViewIsClickable" format="boolean" /> <attr name="cCenterViewIsClickable" format="boolean" /> <attr name="cRightViewIsClickable" format="boolean" /> <attr name="cBackgroundDrawableRes" format="reference" /> <attr name="cIsCenterAlignLeft" format="boolean" /> <attr name="cCenterViewMarginLeft" format="dimension" /> </declare-styleable> </resources>
color.xml
<!-- 白色 --> <color name="white">#FFF</color> <color name="common_pressed">#20000000</color> <!--白色背景按下去的颜色--> <color name="line">#e8e8e8</color>
common_textview_ids.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="cLeftTextId" type="id" /> <item name="cRightTextId" type="id" /> <item name="cCenterTextId" type="id" /> <item name="cCenterBaseLineId" type="id" /> <item name="cLeftTopTextId" type="id" /> <item name="cLeftBottomTextId" type="id" /> <item name="cCenterTopTextId" type="id" /> <item name="cCenterBottomTextId" type="id" /> <item name="cRightTopTextId" type="id" /> <item name="cRightBottomTextId" type="id" /> <item name="cLeftImageViewId" type="id" /> <item name="cRightImageViewId" type="id" /> </resources>
ids.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="sLeftIconId" type="id" /> <item name="sCenterBaseLineId" type="id" /> <item name="sCenterTextId" type="id" /> <item name="sLeftTextId" type="id" /> <item name="sLeftTopTextId" type="id" /> <item name="sLeftBottomTextId" type="id" /> <item name="sLeftBottomTextId2" type="id" /> <item name="sRightTextId" type="id" /> <item name="sRightIconId" type="id" /> </resources>
学习来源:https://github.com/lygttpod/SuperTextView
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!