diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981..fbb6828 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 71789a5..727746b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/com/allen/supertextview/CommonTextViewActivity.java b/app/src/main/java/com/allen/supertextview/CommonTextViewActivity.java new file mode 100644 index 0000000..0edfe03 --- /dev/null +++ b/app/src/main/java/com/allen/supertextview/CommonTextViewActivity.java @@ -0,0 +1,17 @@ +package com.allen.supertextview; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; + +/** + * Created by allen on 2016/11/22. + */ + +public class CommonTextViewActivity extends AppCompatActivity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.common_textview_layout); + } +} diff --git a/app/src/main/java/com/allen/supertextview/MainActivity.java b/app/src/main/java/com/allen/supertextview/MainActivity.java index f81a8b4..cc85dfd 100644 --- a/app/src/main/java/com/allen/supertextview/MainActivity.java +++ b/app/src/main/java/com/allen/supertextview/MainActivity.java @@ -73,7 +73,8 @@ public void onClick(View v) { startActivity(intent); break; case R.id.button6: - intent.setClass(this, ListActivity.class); +// intent.setClass(this, ListActivity.class); + intent.setClass(this, CommonTextViewActivity.class); intent.putExtra("type", 6); startActivity(intent); break; diff --git a/app/src/main/res/layout/common_textview_layout.xml b/app/src/main/res/layout/common_textview_layout.xml new file mode 100644 index 0000000..d6a1bb0 --- /dev/null +++ b/app/src/main/res/layout/common_textview_layout.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/text.xml b/app/src/main/res/layout/text.xml deleted file mode 100644 index 21346cd..0000000 --- a/app/src/main/res/layout/text.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/library/build.gradle b/library/build.gradle index 670afe9..98aacca 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -12,8 +12,8 @@ android { defaultConfig { minSdkVersion 11 targetSdkVersion 24 - versionCode 104 - versionName "1.0.4" + versionCode 105 + versionName "1.0.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/library/src/main/java/com/allen/library/CommonTextView.java b/library/src/main/java/com/allen/library/CommonTextView.java new file mode 100644 index 0000000..759b9bf --- /dev/null +++ b/library/src/main/java/com/allen/library/CommonTextView.java @@ -0,0 +1,829 @@ +package com.allen.library; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.widget.RelativeLayout; +import android.widget.TextView; + +/** + * Created by allen on 2016/11/21. + *

+ * 通用的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 String mLeftTextString; + private String mRightTextString; + private String mCenterTextString; + + private int mLeftTextSize; + private int mRightTextSize; + private int mCenterTextSize; + + private int mLeftTextColor; + private int mCenterTextColor; + private int mRightTextColor; + + 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 mBottomDividerLineMarginLR; + private int mBothDividerLineMarginLR; + + 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 static final int DRAWABLE_LEFT = 11; + private static final int DRAWABLE_TOP = 12; + private static final int DRAWABLE_RIGHT = 13; + private static final int DRAWABLE_BOTTOM = 14; + + + private TextView leftTextView, centerTextView, rightTextView; + + private View topLineView, bottomLineView; + + private RelativeLayout.LayoutParams leftTVParams, centerTVParams, rightTVParams, topLineParams, bottomLineParams; + + private OnCommonTextViewClickListener onCommonTextViewClickListener; + + 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); + 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); + + mLeftTextString = typedArray.getString(R.styleable.CommonTextView_cLeftTextString); + mCenterTextString = typedArray.getString(R.styleable.CommonTextView_cCenterTextString); + mRightTextString = typedArray.getString(R.styleable.CommonTextView_cRightTextString); + + mLeftTextColor = typedArray.getColor(R.styleable.CommonTextView_cLeftTextColor, defaultColor); + mCenterTextColor = typedArray.getColor(R.styleable.CommonTextView_cCenterTextColor, defaultColor); + mRightTextColor = typedArray.getColor(R.styleable.CommonTextView_cRightTextColor, defaultColor); + + mLeftTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cLeftTextSize, defaultSize); + mCenterTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cCenterTextSize, defaultSize); + mRightTextSize = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cRightTextSize, 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); + + mBothDividerLineMarginLR = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBothDividerLineMarginLR, 0); + mTopDividerLineMarginLR = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cTopDividerLineMarginLR, 0); + mBottomDividerLineMarginLR = typedArray.getDimensionPixelSize(R.styleable.CommonTextView_cBottomDividerLineMarginLR, 0); + + 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); + typedArray.recycle(); + } + + /** + * 初始化 + */ + private void init() { + initCommonTextView(); + initLineView(); + initLeftText(); + initCenterText(); + initRightText(); + } + + /** + * 初始化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(); + } + } + }); + } + + /** + * 初始化分割线 + */ + private void initLineView() { + switch (mDividerLineType) { + case NONE: + break; + case TOP: + initTopLineView(mTopDividerLineMarginLR); + break; + case BOTTOM: + initBottomLineView(mBottomDividerLineMarginLR); + break; + case BOTH: + initTopLineView(mBothDividerLineMarginLR); + initBottomLineView(mBothDividerLineMarginLR); + break; + } + + } + + /** + * 设置上边分割线view + * + * @param margin 左右间距 + */ + private void initTopLineView(int margin) { + if (topLineView == null) { + if (topLineParams == null) { + topLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, mDividerLineHeight); + } + topLineParams.addRule(ALIGN_PARENT_TOP, TRUE); + topLineParams.setMargins(margin, 0, margin, 0); + topLineView = new View(mContext); + topLineView.setLayoutParams(topLineParams); + topLineView.setBackgroundColor(mDividerLineColor); + } + addView(topLineView); + } + + /** + * 设置底部分割线view + * + * @param margin 左右间距 + */ + private void initBottomLineView(int margin) { + if (bottomLineView == null) { + if (bottomLineParams == null) { + bottomLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, mDividerLineHeight); + } + bottomLineParams.addRule(ALIGN_PARENT_BOTTOM, TRUE); + bottomLineParams.setMargins(margin, 0, margin, 0); + + bottomLineView = new View(mContext); + bottomLineView.setLayoutParams(bottomLineParams); + bottomLineView.setBackgroundColor(mDividerLineColor); + } + addView(bottomLineView); + } + + + /** + * 初始化左边textView + */ + private void initLeftText() { + if (leftTextView == null) { + if (leftTVParams == null) { + leftTVParams = getParams(leftTVParams); + } + leftTVParams.addRule(CENTER_VERTICAL, TRUE); + leftTVParams.addRule(ALIGN_PARENT_LEFT, TRUE); + leftTVParams.setMargins(mLeftViewPaddingLeft, 0, mLeftViewPaddingRight, 0); + leftTextView = initText(leftTextView, leftTVParams, R.id.cLeftTextId, mLeftTextColor, mLeftTextSize); + leftTextView.setText(mLeftTextString); + leftTextView.setGravity(Gravity.CENTER_VERTICAL); + leftTextView.setLineSpacing(mLeftTextViewLineSpacingExtra, 1.0f); + } + + setDrawable(leftTextView, mLeft_drawableLeft, mLeft_drawableTop, mLeft_drawableRight, mLeft_drawableBottom, mLeftIconDrawablePadding); + + } + + /** + * 初始化中间textView + */ + private void initCenterText() { + if (centerTextView == null) { + if (centerTVParams == null) { + centerTVParams = getParams(centerTVParams); + } + + centerTVParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); + centerTVParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); + centerTVParams.setMargins(mCenterViewPaddingLeft, 0, mCenterViewPaddingRight, 0); + + centerTextView = initText(centerTextView, centerTVParams, R.id.cCenterTextId, mCenterTextColor, mCenterTextSize); + centerTextView.setText(mCenterTextString); + centerTextView.setGravity(Gravity.CENTER); + centerTextView.setLineSpacing(mCenterTextViewLineSpacingExtra, 1.0f); + + } + setDrawable(centerTextView, mCenter_drawableLeft, mCenter_drawableTop, mCenter_drawableRight, mCenter_drawableBottom, mCenterIconDrawablePadding); + + } + + /** + * 初始化右边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.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); + + } + setDrawable(rightTextView, mRight_drawableLeft, mRight_drawableTop, mRight_drawableRight, mRight_drawableBottom, mRightIconDrawablePadding); + + } + + + /** + * 初始化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(1); + textView.setEms(10); + addView(textView); + } + return textView; + } + + /** + * 设置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(String string) { + if (leftTextView == null) { + initLeftText(); + } + leftTextView.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(String string) { + if (centerTextView == null) { + initCenterText(); + } + centerTextView.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(String string) { + if (rightTextView == null) { + initRightText(); + } + rightTextView.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; + } + + /** + * 获取左边textView的值 + * + * @return 返回 + */ + public CharSequence getLeftTextString() { + return leftTextView != null ? leftTextView.getText() : ""; + } + + /** + * 获取中间textView的值 + * + * @return 返回 + */ + public CharSequence getCenterTextString() { + return centerTextView != null ? centerTextView.getText() : ""; + } + + /** + * 获取右边textView的值 + * + * @return 返回 + */ + public CharSequence getRightTextString() { + return rightTextView != null ? rightTextView.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; + } + + /** + * 单位转换工具类 + * + * @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); + } + +} diff --git a/library/src/main/java/com/allen/library/SuperTextView.java b/library/src/main/java/com/allen/library/SuperTextView.java index c077bf9..71f0a7c 100644 --- a/library/src/main/java/com/allen/library/SuperTextView.java +++ b/library/src/main/java/com/allen/library/SuperTextView.java @@ -52,6 +52,13 @@ public class SuperTextView extends RelativeLayout { 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 bottomLineMargin;//下边线的左右边距 private int bothLineMargin;//两条线的左右边距 @@ -127,9 +134,10 @@ public SuperTextView(Context context) { public SuperTextView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; - defaultLinePadding = dip2px(context, 16); +// defaultLinePadding = dip2px(context, 16); defaultPadding = dip2px(context, 16); defaultSize = sp2px(context, 14); + centerSpaceHeight = dip2px(context, 10); getAttr(attrs); initLayout(); @@ -167,6 +175,14 @@ private void getAttr(AttributeSet attrs) { 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); bottomLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBottomLineMargin, defaultLinePadding); bothLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sBothLineMargin, defaultLinePadding); @@ -243,14 +259,14 @@ private void initLayout() { case NONE: break; case TOP: - initTopLine(topLineMargin); + initTopLine(topLineMargin,topLineWidth); break; case BOTTOM: - initBottomLine(bottomLineMargin); + initBottomLine(bottomLineMargin,bottomLineWidth); break; case BOTH: - initTopLine(bothLineMargin); - initBottomLine(bothLineMargin); + initTopLine(bothLineMargin,bothLineWidth); + initBottomLine(bothLineMargin,bothLineWidth); break; } } @@ -259,26 +275,26 @@ private void initLayout() { /** * 初始化上边的线 */ - private void initTopLine(int lineMargin) { + private void initTopLine(int lineMargin,int lineWidth) { View topLine = new View(mContext); - topLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(mContext, 1)); + topLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, lineWidth); topLineParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, TRUE); topLineParams.setMargins(lineMargin, 0, lineMargin, 0); topLine.setLayoutParams(topLineParams); - topLine.setBackgroundColor(defaultLineColor); + topLine.setBackgroundColor(lineColor); addView(topLine); } /** * 初始化下边的线 */ - private void initBottomLine(int lineMargin) { + private void initBottomLine(int lineMargin,int lineWidth) { View bottomLine = new View(mContext); - bottomLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(mContext, 1)); + bottomLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, lineWidth); bottomLineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, TRUE); bottomLineParams.setMargins(lineMargin, 0, lineMargin, 0); bottomLine.setLayoutParams(bottomLineParams); - bottomLine.setBackgroundColor(defaultLineColor); + bottomLine.setBackgroundColor(lineColor); addView(bottomLine); } @@ -309,7 +325,7 @@ public void onClick(View view) { */ private void initCenterBaseLine() { View view = new View(mContext); - centerBaseLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(mContext, 10)); + centerBaseLineParams = new LayoutParams(LayoutParams.MATCH_PARENT, centerSpaceHeight); centerBaseLineParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); view.setId(R.id.sCenterBaseLineId); view.setLayoutParams(centerBaseLineParams); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 99a582a..3524a2d 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -1,5 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + @@ -18,6 +39,8 @@ + + @@ -42,6 +65,7 @@ + @@ -61,5 +85,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/colors.xml b/library/src/main/res/values/colors.xml index 9bd3b6b..506ee6f 100644 --- a/library/src/main/res/values/colors.xml +++ b/library/src/main/res/values/colors.xml @@ -3,5 +3,6 @@ #FFF #20000000 + #e8e8e8 \ No newline at end of file diff --git a/library/src/main/res/values/commom_textview_ids.xml b/library/src/main/res/values/commom_textview_ids.xml new file mode 100644 index 0000000..6aa45bf --- /dev/null +++ b/library/src/main/res/values/commom_textview_ids.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file