Skip to content

Commit e24d508

Browse files
committed
增加左右text支持span
1 parent 689b444 commit e24d508

File tree

7 files changed

+211
-14
lines changed

7 files changed

+211
-14
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 26
4+
compileSdkVersion 27
55
buildToolsVersion "27.0.0"
66
defaultConfig {
77
applicationId "com.study.xuan.easytextview"
88
minSdkVersion 15
9-
targetSdkVersion 26
9+
targetSdkVersion 27
1010
versionCode 1
1111
versionName "1.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

app/src/main/java/com/study/xuan/easytextview/MainActivity.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.support.v7.widget.LinearLayoutManager;
77
import android.support.v7.widget.RecyclerView;
8+
import android.text.SpannableStringBuilder;
9+
import android.text.Spanned;
10+
import android.text.style.ForegroundColorSpan;
11+
import android.text.style.UnderlineSpan;
812
import android.view.LayoutInflater;
913
import android.view.View;
1014
import android.view.ViewGroup;
15+
import android.widget.TextView;
1116

1217
import com.study.xuan.library.widget.EasyTextView;
1318

@@ -27,6 +32,23 @@ protected void onCreate(Bundle savedInstanceState) {
2732
super.onCreate(savedInstanceState);
2833
setContentView(R.layout.activity_main);
2934
textView = (EasyTextView) findViewById(R.id.easyText);
35+
SpannableStringBuilder stringBuilder = new SpannableStringBuilder("呵呵呵呵呵");
36+
stringBuilder.setSpan(new UnderlineSpan(), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
37+
SpannableStringBuilder insert = new SpannableStringBuilder("你好啊啊啊");
38+
insert.setSpan(new UnderlineSpan(), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
39+
stringBuilder.insert(0, insert);
40+
/*TextView text = (TextView) findViewById(R.id.text);
41+
SpannableStringBuilder stringBuilder = new SpannableStringBuilder("呵呵呵呵呵");
42+
SpannableStringBuilder insert = new SpannableStringBuilder("你好啊啊啊");
43+
insert.setSpan(new UnderlineSpan(), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
44+
stringBuilder.insert(0, insert);
45+
text.setText(stringBuilder);*/
46+
//textView.setTextRight(insert);
47+
textView.setTextRight("啊啊啊啊");
48+
textView.addSpanLeft(new ForegroundColorSpan(Color.RED), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
49+
textView.spanRight(new ForegroundColorSpan(Color.RED), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
50+
.spanRight(new ForegroundColorSpan(Color.YELLOW), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
51+
.build();
3052
textView.setOnClickListener(new View.OnClickListener() {
3153
@Override
3254
public void onClick(View v) {
@@ -36,7 +58,7 @@ public void onClick(View v) {
3658

3759
}
3860
});
39-
final List<String> data = new ArrayList();
61+
/*final List<String> data = new ArrayList();
4062
for (int i = 0; i < 100; i++) {
4163
data.add("第" + i + "个");
4264
}
@@ -60,7 +82,7 @@ public int getItemCount() {
6082
return data.size();
6183
}
6284
});
63-
85+
*/
6486
}
6587

6688
class ViewHolder extends RecyclerView.ViewHolder {

app/src/main/res/layout/activity_main.xml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,33 @@
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
88
android:gravity="center"
9+
android:orientation="vertical"
910
>
1011

1112
<com.study.xuan.library.widget.EasyTextView
13+
android:id="@+id/easyText"
1214
android:layout_width="wrap_content"
1315
android:layout_height="wrap_content"
1416
app:textRight="@string/icon_font_home"
1517
android:text="@string/icon_font_home"
1618
app:textLeft="左边的"
17-
android:textSize="10dp"
19+
android:textSize="30dp"
1820
app:textPadding="20dp"
19-
app:textLeftSize="30dp"
21+
app:textLeftSize="34dp"
22+
android:background="@color/colorPrimary"
2023
/>
2124

22-
<android.support.v7.widget.RecyclerView
25+
<TextView
26+
android:layout_marginTop="20dp"
27+
android:id="@+id/text"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content" />
30+
31+
<!--<android.support.v7.widget.RecyclerView
2332
android:id="@+id/rcy"
2433
android:layout_width="match_parent"
2534
android:layout_height="match_parent"
2635
android:visibility="gone"
27-
/>
36+
/>-->
2837

2938
</LinearLayout>

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 26
4+
compileSdkVersion 27
55
buildToolsVersion "27.0.0"
66

77
defaultConfig {
88
minSdkVersion 15
9-
targetSdkVersion 26
9+
targetSdkVersion 27
1010
versionCode 1
1111
versionName "1.0"
1212

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.study.xuan.library.span;
2+
3+
import android.text.SpannableStringBuilder;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Author : xuan.
9+
* Date : 2017/11/23.
10+
* Description :字符串多样式的SpannableString
11+
*/
12+
13+
public class MultiSpannableString extends SpannableStringBuilder {
14+
public MultiSpannableString() {
15+
}
16+
17+
public MultiSpannableString(CharSequence source) {
18+
super(source);
19+
}
20+
21+
/**
22+
* 支持多样式的SpannableString
23+
*/
24+
public void setMultiSpans(int start, int end, int flags, Object... objects) {
25+
for (Object item : objects) {
26+
setSpan(item, start, end, flags);
27+
}
28+
}
29+
30+
public void setMultiSpans(List<Object> objects, int start, int end, int flags) {
31+
for (Object item : objects) {
32+
setSpan(item, start, end, flags);
33+
}
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.study.xuan.library.span;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Author : xuan.
8+
* Date : 18-3-23.
9+
* Description : the file description
10+
*/
11+
public class SpanContainer {
12+
public List<Object> spans;
13+
public int start;
14+
public int end;
15+
public int flag;
16+
17+
public SpanContainer(List<Object> spans, int start, int end, int flag) {
18+
this.spans = spans;
19+
this.start = start;
20+
this.end = end;
21+
this.flag = flag;
22+
}
23+
24+
public SpanContainer(Object spans, int start, int end, int flag) {
25+
this.spans = new ArrayList<>();
26+
this.spans.add(spans);
27+
this.start = start;
28+
this.end = end;
29+
this.flag = flag;
30+
}
31+
}

library/src/main/java/com/study/xuan/library/widget/EasyTextView.java

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
import com.study.xuan.library.R;
1919
import com.study.xuan.library.span.EasyVerticalCenterSpan;
20+
import com.study.xuan.library.span.SpanContainer;
2021
import com.study.xuan.shapebuilder.shape.ShapeBuilder;
2122

23+
import java.util.ArrayList;
24+
import java.util.List;
25+
2226
import static android.graphics.drawable.GradientDrawable.RECTANGLE;
2327

2428
/**
@@ -29,6 +33,7 @@
2933
* 2.iconFont配合textLeft,textRight,textPadding,iconColor等
3034
* 3.支持不同左中右不同字号垂直居中
3135
* 4.支持左中上分别设置Selector,不要设置TextColor,会覆盖(一个TextView)
36+
* 5.支持左右text设置span
3237
*/
3338

3439
public class EasyTextView extends TextView {
@@ -40,8 +45,8 @@ public class EasyTextView extends TextView {
4045
private int mStrokeWidth;
4146
private int mSoild;
4247
private float mTextPadding;
43-
private String mTextLeft;
44-
private String mTextRight;
48+
private CharSequence mTextLeft;
49+
private CharSequence mTextRight;
4550
private ColorStateList mIconColor = null;
4651
private int mCurIconColor;
4752
private String iconString;
@@ -51,6 +56,8 @@ public class EasyTextView extends TextView {
5156
private int mCurRightColor;
5257
private float mLeftSize;
5358
private float mRightSize;
59+
private List<SpanContainer> leftContainer;
60+
private List<SpanContainer> rightContainer;
5461
//icon的index
5562
private int iconIndex = 0;
5663

@@ -105,6 +112,7 @@ private void initIconFont() {
105112
* ==============
106113
*/
107114
if (mTextPadding != 0) {
115+
//设置字和icon间距
108116
if (!TextUtils.isEmpty(mTextLeft)) {
109117
AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan((int) mTextPadding);
110118
stringBuilder.setSpan(sizeSpan, iconIndex - 1, iconIndex, Spanned
@@ -207,6 +215,22 @@ private void initIconFont() {
207215
mCurIconColor = getCurrentTextColor();
208216
}
209217

218+
if (leftContainer != null) {
219+
for (SpanContainer container : leftContainer) {
220+
for (Object o : container.spans) {
221+
stringBuilder.setSpan(o, container.start, container.end, container.flag);
222+
}
223+
}
224+
}
225+
if (rightContainer != null) {
226+
int start = mTextPadding == 0 ? iconIndex + 1 : iconIndex + 2;
227+
for (SpanContainer container : rightContainer) {
228+
for (Object o : container.spans) {
229+
stringBuilder.setSpan(o, start + container.start, start + container.end, container.flag);
230+
}
231+
}
232+
}
233+
210234
setText(stringBuilder);
211235
}
212236

@@ -289,15 +313,15 @@ public void setIconColor(int color) {
289313
/**
290314
* 设置左文案
291315
*/
292-
public void setTextLeft(String textLeft) {
316+
public void setTextLeft(CharSequence textLeft) {
293317
this.mTextLeft = textLeft;
294318
build();
295319
}
296320

297321
/**
298322
* 设置右文案
299323
*/
300-
public void setTextRight(String textRight) {
324+
public void setTextRight(CharSequence textRight) {
301325
this.mTextRight = textRight;
302326
build();
303327
}
@@ -341,6 +365,38 @@ public void setIcon(String iconText) {
341365
this.iconString = iconText;
342366
build();
343367
}
368+
369+
/**
370+
* 设置左边文字为多个span
371+
*/
372+
public void addSpanLeft(List<Object> objects, int start, int end, int flags) {
373+
spanLeft(objects, start, end, flags);
374+
build();
375+
}
376+
377+
/**
378+
* 设置左边文字为span
379+
*/
380+
public void addSpanLeft(Object object, int start, int end, int flags) {
381+
spanLeft(object, start, end, flags);
382+
build();
383+
}
384+
385+
/**
386+
* 设置右边文字为多个span
387+
*/
388+
public void addSpanRight(List<Object> objects, int start, int end, int flags) {
389+
spanRight(objects, start, end, flags);
390+
build();
391+
}
392+
393+
/**
394+
* 设置右边文字为span
395+
*/
396+
public void addSpanRight(Object object, int start, int end, int flags) {
397+
spanRight(object, start, end, flags);
398+
build();
399+
}
344400
//=================================链式调用##需要最后调用build()==================================
345401

346402
/**
@@ -407,6 +463,50 @@ public EasyTextView icon(String iconText) {
407463
return this;
408464
}
409465

466+
/**
467+
* 设置右边文字为多个span
468+
*/
469+
public EasyTextView spanRight(List<Object> objects, int start, int end, int flags) {
470+
if (rightContainer == null) {
471+
rightContainer = new ArrayList<>();
472+
}
473+
this.rightContainer.add(new SpanContainer(objects, start, end, flags));
474+
return this;
475+
}
476+
477+
/**
478+
* 设置右边文字为span
479+
*/
480+
public EasyTextView spanRight(Object object, int start, int end, int flags) {
481+
if (rightContainer == null) {
482+
rightContainer = new ArrayList<>();
483+
}
484+
this.rightContainer.add(new SpanContainer(object, start, end, flags));
485+
return this;
486+
}
487+
488+
/**
489+
* 设置左边文字为多个span
490+
*/
491+
public EasyTextView spanLeft(List<Object> objects, int start, int end, int flags) {
492+
if (leftContainer == null) {
493+
leftContainer = new ArrayList<>();
494+
}
495+
this.leftContainer.add(new SpanContainer(objects, start, end, flags));
496+
return this;
497+
}
498+
499+
/**
500+
* 设置左边文字为span
501+
*/
502+
public EasyTextView spanLeft(Object object, int start, int end, int flags) {
503+
if (leftContainer == null) {
504+
leftContainer = new ArrayList<>();
505+
}
506+
this.leftContainer.add(new SpanContainer(object, start, end, flags));
507+
return this;
508+
}
509+
410510
/**
411511
* 防止重复初始化,最后调用build
412512
*/

0 commit comments

Comments
 (0)