Skip to content

Commit

Permalink
新增设置左中右text的background
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyao committed Jul 27, 2017
1 parent 56cba60 commit 3f39b50
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions app/src/main/res/drawable-xxhdpi/shape_cycle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>

<solid android:color="@color/red_btn" />

<size
android:width="20dp"
android:height="20dp" />
</shape>
14 changes: 14 additions & 0 deletions app/src/main/res/drawable-xxhdpi/shape_cycle_rectangle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="@color/red_btn" />

<corners android:radius="5dp" />

<padding
android:bottom="2dp"
android:left="4dp"
android:right="4dp"
android:top="2dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="@color/white" />

<corners android:radius="2dp" />

<stroke
android:width="1dp"
android:color="@color/colorPrimary" />
<padding
android:bottom="3dp"
android:left="10dp"
android:right="10dp"
android:top="3dp" />
</shape>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-xxhdpi/shape_cycle_small.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="@color/red_btn" />

<size
android:width="15dp"
android:height="15dp" />
</shape>
9 changes: 4 additions & 5 deletions app/src/main/res/drawable/thumb.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 按钮的选择器,可以设置按钮在不同状态下的时候,按钮不同的颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<?xml version="1.0" encoding="utf-8"?><!-- 按钮的选择器,可以设置按钮在不同状态下的时候,按钮不同的颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true" android:drawable="@drawable/green_thumb" />
<item android:drawable="@drawable/gray_thumb" />
<item android:drawable="@drawable/green_thumb" android:state_checked="true" />
<item android:drawable="@drawable/gray_thumb" />
</selector>
9 changes: 4 additions & 5 deletions app/src/main/res/drawable/track.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 底层下滑条的样式选择器,可控制Switch在不同状态下,底下下滑条的颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_track" />
<item android:drawable="@drawable/gray_track" />
<?xml version="1.0" encoding="utf-8"?><!-- 底层下滑条的样式选择器,可控制Switch在不同状态下,底下下滑条的颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/green_track" android:state_checked="true" />
<item android:drawable="@drawable/gray_track" />

</selector>
56 changes: 55 additions & 1 deletion app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
stv:sLeftIconRes="@drawable/not_bank_card_icon"
stv:sLeftTopTextString="银行卡支付"
stv:sLeftViewGravity="left_center"

stv:sRightCheckBoxRes="@drawable/circular_check_bg" />


Expand All @@ -66,6 +65,22 @@
stv:sLeftTopTextString="张三"
stv:sLeftViewGravity="left_center" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="80dp"
stv:sCenterBottomTextColor="#979b9e"
stv:sCenterBottomTextString="关注"
stv:sCenterSpaceHeight="10dp"
stv:sCenterTopTextString="666666"
stv:sLeftBottomTextColor="#979b9e"
stv:sLeftBottomTextString="点赞"
stv:sLeftTopTextString="100000"
stv:sLeftViewMarginLeft="30dp"
stv:sRightBottomTextColor="#979b9e"
stv:sRightBottomTextString="粉丝"
stv:sRightTopTextString="888888"
stv:sRightViewMarginRight="30dp" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="60dp"
Expand All @@ -91,6 +106,45 @@
stv:sThumbResource="@drawable/thumb"
stv:sTrackResource="@drawable/track" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sCenterTextBackground="@drawable/shape_cycle"
stv:sCenterTextColor="@color/white"
stv:sCenterTextSize="13sp"
stv:sCenterTextString="6"
stv:sCenterViewGravity="left_center"
stv:sLeftTextString="功能介绍" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sCenterTextColor="@color/white"
stv:sCenterTvDrawableLeft="@mipmap/red_circle"
stv:sCenterViewGravity="left_center"
stv:sLeftIconRes="@drawable/uv"
stv:sLeftTextString="钱包" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sLeftTextString="特别关心"
stv:sRightIconRes="@drawable/arrow_right_red"
stv:sRightTextBackground="@drawable/shape_cycle_rectangle"
stv:sRightTextColor="@color/white"
stv:sRightTextSize="10sp"
stv:sRightTextString="NEW" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sLeftTextString="微信"
stv:sRightTextBackground="@drawable/shape_cycle_rectangle_download"
stv:sRightTextColor="@color/colorPrimary"
stv:sRightTextSize="10sp"
stv:sRightTextString="下载" />


<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
Expand Down
Binary file added app/src/main/res/mipmap-hdpi/red_circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 208
versionName "2.0.8"
versionCode 209
versionName "2.0.9"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
30 changes: 30 additions & 0 deletions library/src/main/java/com/allen/library/SuperTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public class SuperTextView extends RelativeLayout {
private boolean mRightTextBold;
private boolean mRightBottomTextBold;

private Drawable mLeftTextBackground;
private Drawable mCenterTextBackground;
private Drawable mRightTextBackground;

private Drawable mLeftTvDrawableLeft;
private Drawable mLeftTvDrawableRight;

Expand Down Expand Up @@ -418,6 +422,11 @@ private void getAttr(AttributeSet attrs) {
mRightTopTextBold = typedArray.getBoolean(R.styleable.SuperTextView_sRightTopTextIsBold, false);
mRightTextBold = typedArray.getBoolean(R.styleable.SuperTextView_sRightTextIsBold, false);
mRightBottomTextBold = typedArray.getBoolean(R.styleable.SuperTextView_sRightBottomTextIsBold, false);

mLeftTextBackground = typedArray.getDrawable(R.styleable.SuperTextView_sLeftTextBackground);
mCenterTextBackground = typedArray.getDrawable(R.styleable.SuperTextView_sCenterTextBackground);
mRightTextBackground = typedArray.getDrawable(R.styleable.SuperTextView_sRightTextBackground);

//////////////////////////////////////////////
useRipple = typedArray.getBoolean(R.styleable.SuperTextView_sUseRipple, true);
mBackground_drawable = typedArray.getDrawable(R.styleable.SuperTextView_sBackgroundDrawableRes);
Expand Down Expand Up @@ -612,10 +621,12 @@ private void initLeftTextView() {
setDefaultTextIsBold(leftView, mLeftTopTextBold, mLeftTextBold, mLeftBottomTextBold);
setDefaultGravity(leftView, mLeftGravity);
setDefaultDrawable(leftView.getCenterTextView(), mLeftTvDrawableLeft, mLeftTvDrawableRight, mTextViewDrawablePadding);
setDefaultBackground(leftView.getCenterTextView(), mLeftTextBackground);

addView(leftView);
}


/**
* 初始化CenterTextView
*/
Expand Down Expand Up @@ -646,6 +657,7 @@ private void initCenterTextView() {
setDefaultTextIsBold(centerView, mCenterTopTextBold, mCenterTextBold, mCenterBottomTextBold);
setDefaultGravity(centerView, mCenterGravity);
setDefaultDrawable(centerView.getCenterTextView(), mCenterTvDrawableLeft, mCenterTvDrawableRight, mTextViewDrawablePadding);
setDefaultBackground(centerView.getCenterTextView(), mCenterTextBackground);

addView(centerView);
}
Expand Down Expand Up @@ -674,6 +686,7 @@ private void initRightTextView() {
setDefaultTextIsBold(rightView, mRightTopTextBold, mRightTextBold, mRightBottomTextBold);
setDefaultGravity(rightView, mRightGravity);
setDefaultDrawable(rightView.getCenterTextView(), mRightTvDrawableLeft, mRightTvDrawableRight, mTextViewDrawablePadding);
setDefaultBackground(rightView.getCenterTextView(), mRightTextBackground);

addView(rightView);
}
Expand Down Expand Up @@ -904,6 +917,23 @@ public void setDefaultDrawable(TextView textView, Drawable drawableLeft, Drawabl
textView.setCompoundDrawablePadding(drawablePadding);
}

/**
* 设置textView的背景,用户传入drawable实现圆角之类的样式
*
* @param textView
* @param background
*/
private void setDefaultBackground(TextView textView, Drawable background) {
if (background != null) {
textView.setVisibility(VISIBLE);
if (Build.VERSION.SDK_INT < 16) {
textView.setBackgroundDrawable(background);
} else {
textView.setBackground(background);
}
}
}

/**
* 初始化分割线
*/
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
<attr name="sRightTopTextIsBold" format="boolean" />
<attr name="sRightTextIsBold" format="boolean" />
<attr name="sRightBottomTextIsBold" format="boolean" />

<attr name="sLeftTextBackground" format="reference" />
<attr name="sCenterTextBackground" format="reference" />
<attr name="sRightTextBackground" format="reference" />

<!-- -->

<attr name="sLeftIconRes" format="reference" />
Expand Down

0 comments on commit 3f39b50

Please sign in to comment.