Skip to content

Commit

Permalink
新增点击时候自定义选中和默认背景效果及中间布局左对齐属性
Browse files Browse the repository at this point in the history
  • Loading branch information
lygttpod committed Jan 19, 2017
1 parent cdb2fa4 commit 5312109
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 17 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.

8 changes: 8 additions & 0 deletions app/src/main/res/drawable-xxhdpi/selector_red.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/gray_btn"
android:state_pressed="true" />
<item
android:drawable="@color/red_btn" />
</selector>
32 changes: 32 additions & 0 deletions app/src/main/res/layout/common_textview_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,38 @@
ctv:cRightTextString=""
ctv:cUseRipple="true" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cBackgroundDrawableRes="@drawable/selector_red"
ctv:cCenterTextColor="@android:color/darker_gray"
ctv:cLeftTextString="自定义背景点击效果"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cCenterTextString="张姐专业广场舞"
ctv:cCenterViewMarginLeft="200dp"
ctv:cIsCenterAlignLeft="true"
ctv:cLeftTextString="领队" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cCenterTextString="九眼龙天天广场A座"
ctv:cCenterViewMarginLeft="200dp"
ctv:cIsCenterAlignLeft="true"
ctv:cLeftTextString="活动地点" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cCenterTextString="唐古拉风暴"
ctv:cCenterViewMarginLeft="200dp"
ctv:cIsCenterAlignLeft="true"
ctv:cLeftTextString="舞动歌单" />

</LinearLayout>


Expand Down
28 changes: 18 additions & 10 deletions app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@
<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sLeftTextSize="14dip"
stv:sLeftTextString="客服热线"
stv:sRightTextColor="#ff0000"
stv:sRightTextSize="12dp"
stv:sLeftTextSize="14dip"
stv:sRightTextString="4006179999"
stv:sRightTextStringRightIconRes="@drawable/arrow_right_red"
stv:sRightTextStringRightIconResPadding="8dip"
stv:sRightTextString="4006179999"
stv:sUseRipple="true" />

<com.allen.library.SuperTextView
Expand All @@ -108,20 +108,28 @@
<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sLeftTextString="自定义图片大小"
stv:sRightTextString="20dp"
stv:sLeftIconWidth="25dp"
stv:sLeftIconHeight="25dp"
stv:sLeftIconRes="@drawable/ic_hint_music" />
stv:sLeftIconRes="@drawable/ic_hint_music"
stv:sLeftIconWidth="25dp"
stv:sLeftTextString="自定义图片大小"
stv:sRightTextString="20dp" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sLeftTextString="自定义图片大小"
stv:sRightTextString="30dp"
stv:sLeftIconWidth="30dp"
stv:sLeftIconHeight="30dp"
stv:sLeftIconRes="@drawable/ic_update_password" />
stv:sLeftIconRes="@drawable/ic_update_password"
stv:sLeftIconWidth="30dp"
stv:sLeftTextString="自定义图片大小"
stv:sRightTextString="30dp" />

<com.allen.library.SuperTextView
android:layout_width="match_parent"
android:layout_height="50dp"
stv:sBackgroundDrawableRes="@drawable/selector_red"
stv:sLeftTextString="自定义背景点击效果"
stv:sRightTextStringRightIconRes="@drawable/arrow_right_red" />

</LinearLayout>
</ScrollView>

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="gray_btn">#9e9e9e</color>
<color name="red_btn">#fd5353</color>
</resources>
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 24
versionCode 110
versionName "1.1.0"
versionCode 111
versionName "1.1.1"

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

Expand Down
31 changes: 27 additions & 4 deletions library/src/main/java/com/allen/library/CommonTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public class CommonTextView extends RelativeLayout {

private OnCommonTextViewClickListener onCommonTextViewClickListener;

private Drawable mBackground_drawable;
private boolean mIsCenterAlignLeft = false;
private int mCenterViewMarginLeft;

public CommonTextView(Context context) {
this(context, null);
Expand Down Expand Up @@ -300,6 +303,11 @@ private void getAttr(AttributeSet attrs) {
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();
}

Expand Down Expand Up @@ -364,6 +372,9 @@ public void onClick(View v) {
}
}
});
if (mBackground_drawable != null) {
this.setBackgroundDrawable(mBackground_drawable);
}
}

/**
Expand Down Expand Up @@ -558,17 +569,29 @@ private void initLeftBottomText() {
private void initCenterText() {
if (centerTextView == null) {
if (centerTVParams == null) {
centerTVParams = getParams(centerTVParams);
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);
centerTVParams.setMargins(mCenterViewPaddingLeft, 0, mCenterViewPaddingRight, 0);

centerTextView = initText(centerTextView, centerTVParams, R.id.cCenterTextId, mCenterTextColor, mCenterTextSize);
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);
setTextViewGravity(centerTextView, mCenterTextViewGravity);

if (mCenterViewIsClickable) {
centerTextView.setOnClickListener(new OnClickListener() {
@Override
Expand Down
7 changes: 7 additions & 0 deletions library/src/main/java/com/allen/library/SuperTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public class SuperTextView extends RelativeLayout {
private boolean mLeftBottomViewIsClickable = false;
private boolean mLeftBottomView2IsClickable = false;

private Drawable mBackground_drawable;

public SuperTextView(Context context) {
super(context);
}
Expand Down Expand Up @@ -236,6 +238,8 @@ private void getAttr(AttributeSet attrs) {
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();
}

Expand Down Expand Up @@ -337,6 +341,9 @@ public void onClick(View view) {
if (useRipple) {
this.setBackgroundResource(R.drawable.selector_white);
}
if (mBackground_drawable != null) {
this.setBackgroundDrawable(mBackground_drawable);
}
}


Expand Down
6 changes: 6 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
<attr name="sLeftBottomViewIsClickable" format="boolean" />
<attr name="sLeftBottomView2IsClickable" format="boolean" />

<attr name="sBackgroundDrawableRes" format="reference" />

</declare-styleable>

<declare-styleable name="CommonTextView">
Expand Down Expand Up @@ -221,6 +223,10 @@
<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>

0 comments on commit 5312109

Please sign in to comment.