基于iconfont拓展的TextView(支持左右设置Text,设置Shape,设置span等)
项目介绍
还在苦于iconfont左右两边还需要设置文字只能加两个TextView?还在苦于需要设置各种大同小异的边框而new Shape文件吗?还在苦于设置点击效果而多种样式吗?还在苦于总用LinearLayout包一层?(代码洁癖患者~)。
一个基于iconfont拓展的支持多种功能的轻量级TextView,减少布局嵌套,减少定义shape文件,基本涵盖常规需要多个TextView实现的功能。
支持的特性
使用简单链式调用支持给iconfont左右设置文字支持xml中直接设置常用shape的所有属性支持分别设置iconfont,左文字,右文字的颜色(Selector也可以)支持分别设置iconfont,左文字,右文字字号支持文字和iconfont居中支持左右文字设置多个span支持设置iconfont和左右文字的padding丰富的api,所有属性都支持xml和java调用
源码地址
EasyTextView
欢迎提issues和PR
Download
Step1
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url
'https://jitpack.io' }
}
}
Step2
Step 2. Add the dependency
dependencies
{
implementation 'com.github.DrownCoder:EasyTextView:v1.0'
}
效果
使用
xml属性
<
com.study.xuan.library.widget.EasyTextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
""
android:textColor=
"#494949"
android:textSize=
"14dp"
app:iconColor=
"@android:color/holo_red_light"
app:strokeColor=
"@android:color/holo_red_light"
app:strokeWidth=
"1dp"
app:textPadding=
"5dp"
app:textRight=
"关注"
app:totalRadius=
"12dp" />
<declare-styleable name=
"EasyTextView">
<attr name=
"shapeType" format=
"integer" />
<attr name=
"totalRadius" format=
"dimension"/>
<attr name=
"radiusTopLeft" format=
"dimension"/>
<attr name=
"radiusBottomLeft" format=
"dimension"/>
<attr name=
"radiusTopRight" format=
"dimension"/>
<attr name=
"radiusBottomRight" format=
"dimension"/>
<attr name=
"strokeColor" format=
"color"/>
<attr name=
"strokeWidth" format=
"dimension" />
<attr name=
"soildBac" format=
"color"/>
<attr name=
"textPadding" format=
"dimension"/>
<attr name=
"textLeft" format=
"string"/>
<attr name=
"textRight" format=
"string"/>
<attr name=
"iconColor" format=
"reference|color"/>
<attr name=
"textLeftColor" format=
"reference|color"/>
<attr name=
"textRightColor" format=
"reference|color"/>
<attr name=
"textLeftSize" format=
"dimension"/>
<attr name=
"textRightSize" format=
"dimension"/>
</declare-styleable>
java Api
//少属性可以直接设置
etvGet
.setTextRight(已关注)
//多属性可以链式调用,减少重复绘制,节省性能,注意最后build()
etvGet
.strokeWidth(DensityUtils
.dp2px(context,
1))
.strokeColor(Color
.parseColor(
"#ffe849"))
.solid(Color
.parseColor(
"#e8264a"))
.icon(context
.getText(R
.string.icon_font_check)
.toString())
.textRight(
"关注")
.build()
/**
* 非链式调用api
*/
public void setType(
int type);
public void setStrokeWidth(
int value);
public void setStrokeColor(@ColorInt
int color);
public void setSolid(
int soild);
public void setIconColor(
int color);
public void setTextLeft(CharSequence textLeft);
public void setTextRight(CharSequence textRight);
public void setTextLeftColor(
int color);
public void setTextRightColor(
int color);
public void setTextLeftSize(
float leftSize);
public void setTextRightSize(
float rightSize);
public void setIcon(String iconText);
public void clearSpan();
public void addSpanLeft(Object object,
int start,
int end,
int flags);
public void addSpanLeft(List<Object> objects,
int start,
int end,
int flags);
public void addSpanRight(List<Object> objects,
int start,
int end,
int flags);
public void addSpanRight(Object object,
int start,
int end,
int flags);
More
动态设置Shape,由SupperShape实现,可以动态通过java代码给任何View设置Shape,不需要再写shape.xml文件,欢迎使用。 本项目仅基于TextView进行封装,其他View可按照需求进行封装,原理比较简单,大家可以查看源码。 详细使用说明参考Wiki