SuperTextView

10 篇文章 0 订阅

文章目录

  • 1、功能描述

  • 2、效果图

  • 3、如何使用

    • 3.2.1、布局中使用

    • 3.2.2、在代码中使用

    • 3.2.3、属性介绍

    • 3.1、 Android Studio导入方法

    • 3.2、 项目中如何使用

  • 4、 实现原理

    • 4.1、需求分析

    • 4.2、代码封装

    • 4.3、总结

1、功能描述

你还在为类似设置页面的样式(图+文字+图)每次写一大堆布局文件而烦恼吗?快来看看SuperTextView吧,这里有你想要实现的任何样式,而你要做的仅仅是引入几行代码。SuperTextView是一个功能强大的布局View,可以满足日常大部分布局样式,开发者可以自行组合属性配置出属于自己风格的样式!可能描述起来没有概念,还是直接看效果图吧!

2、 效果图

Paste_Image.png

3、如何使用

Android Studio导入方法,添加Gradle依赖

先在项目根目录的 build.gradle 的 repositories 添加:

     allprojects {
         repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

然后在dependencies添加:

        dependencies {
        ...
        compile 'com.github.lygttpod:SuperTextView:2.0.1'
        }

3.2、项目中如何使用

3.2.1、布局中如何使用(示例中只列出部分属性,开发者可根据具体需求使用其他属性)
        <com.allen.supertextviewlibrary.SuperTextView
                android:id="@+id/super_tv"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                stv:sLeftBottomTextColor2="@color/colorAccent"
                stv:sLeftBottomTextString="招商银行(8888)"
                stv:sLeftBottomTextString2="限额说明>>"
                stv:sLeftIconRes="@drawable/bank_zhao_shang"
                stv:sLeftTopTextString="银行卡支付"
                stv:sRightCheckBoxRes="@drawable/circular_check_bg"
                stv:sRightCheckBoxShow="true"
                stv:sLineShow="bottom"
                 />
        注意:1、上下的线可以通过 sLineShow 设置:有四种显示方式 none,top,bottom,both  2、通过设置 sUseRipple=true 开启水波效果
3.2.2、代码中如何使用
    /**
     * 可以通过链式设置大部分常用的属性值
     */
        
superTextView.setLeftIcon(drawable)
        .setLeftString("")
        .setLeftTVColor(0)
        .setLeftTopString("")
        .setLeftTopTVColor(0)
        .setLeftBottomString("")
        .setLeftBottomTVColor(0)
        .setLeftBottomString2("")
        .setLeftBottomTVColor2(0)
        .setRightString("")
        .setRightTVColor(0)
        .setCbChecked(true)
        .setCbBackground(drawable)
        .setRightIcon(drawable)
        .setRightString("")
        .setRightTVColor(0)
        .setLeftString("");
点击事件(可根据需求选择实现单个或者多个点击事件,需要配合xml添加是否允许点击的属性,详情见属性参数的意义)
       
superTextView.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener()

{
    @Override
    public void onSuperTextViewClick () {
    super.onSuperTextViewClick();      //do something
}
    @Override
    public void onLeftTopClick () {
    super.onLeftTopClick();              //do something
}
    @Override
    public void onLeftBottomClick () {
    super.onLeftBottomClick();              //do something
}
    @Override
    public void onLeftBottomClick2 () {
    super.onLeftBottomClick2();            //do something
}
});
                注意:点击事件需要配合属性值使用                sLeftTopViewIsClickable= true                sLeftBottomViewIsClickable= true                sLeftBottomView2IsClickable= true
使用第三方库(Picasso或者Glide)加载网络图片
         
Picasso.with(this).load(url) 
.placeholder(R.drawable.head_default)  
.into((ImageView) superTextView.getView(SuperTextView.leftImageViewId));
3.2.3、属性说明(以下属性全部可以通过xml文件配置和代码进行设置)
属性名字段描述
sLeftIconResreference左边图标资源
sRightIconResreference右边图标资源
sLeftIconWidthdimension左边图标的宽度
sLeftIconHeightdimension左边图标的高度
sRightIconWidthdimension右边图标的高度
sRightIconHeightdimension右边图标的高度
sRightCheckBoxResreference右边CheckBox图标资源
sLeftTextStringstring左边文字字符串
sLeftTopTextStringstring左上文字字符串
sLeftBottomTextStringstring左下文字字符串
sLeftBottomTextString2string左下第二个文字字符串
sCenterTextStringstring中间文字字符串
sRightTextStringstring右边文字字符串
sTopLineMargindimension顶部横线的Margin
sTopLineMarginLeftdimension顶部横线的MarginLeft
sTopLineMarginRightdimension顶部横线的MarginRight
sBottomLineMargindimension底部横线的Margin
sBottomLineMarginLeftdimension底部横线的MarginLeft
sBottomLineMarginRightdimension底部横线的MarginRight
sBothLineMargindimension上下两条横线的Margin
sBothLineMarginLeftdimension上下两条横线的MarginLeft
sBothLineMarginRightdimension上下两条横线的MarginRight
sCenterSpaceHeightdimensiontop和bottom文字间的高度
sLeftIconMarginLeftdimension左边图标MarginLeft
sLeftTextMarginLeftdimension左边文字MarginLeft
sLeftTopTextMarginLeftdimension左上文字MarginLeft
sLeftBottomTextMarginLeftdimension左下文字MarginLeft
sLeftBottomTextMarginLeft2dimension左下第二个文字MarginLeft
sRightTextStringRightIconResreference右边文字的右边图标资源
sRightTextStringRightIconResPaddingdimension右边文字的右边图标的Padding
sRightIconMarginRightdimension右边icon的MarginRight
sRightTextMarginRightdimension右边文字的MarginRight
sRightCheckBoxMarginRightdimension右边CheckBox的MarginRight
sRightCheckBoxShowboolean右边的CheckBox是否显示
sIsCheckedboolean是否选中
sUseRippleboolean是否使用点击出现波纹效果
sLeftTextSizedimension左边文字的字体大小
sLeftTopTextSizedimension左上文字的字体大小
sLeftBottomTextSizedimension左下文字的字体大小
sLeftBottomTextSize2dimension左下第二个文字的字体大小
sRightTextSizedimension右边文字的字体大小
sCenterTextSizedimension中间文字的字体大小
sBackgroundColorcolor背景颜色
sLeftTextColorcolor左边文字颜色
sLeftTopTextColorcolor左上文字颜色
sLeftBottomTextColorcolor左下文字颜色
sLeftBottomTextColor2color左下第二个文字颜色
sRightTextColorcolor右边文字颜色
sCenterTextColorcolor中间文字颜色
sIsSingLinesboolean是否单行显示
sMaxLinesinteger允许的最大行数
sMaxEmsinteger允许的最多字数
sLineShowenum分割线的显示方式none、top、bottom、both
sBothLineWidthdimension两条分割线的宽度
sTopLineWidthdimension上边分割线的宽度
sBottomLineWidthdimension下边分割线的宽度
sLineColorcolor分割线的颜色
sLeftTopViewIsClickableboolean左上view是否可点击
sLeftBottomViewIsClickableboolean左下view是否可点击
sLeftBottomView2IsClickableboolean左下第二个view是否可点击
sBackgroundDrawableResreference背景资源

4、实现原理

4.1、需求分析

黑格尔曾说过:存在即合理。SuperTextView的出现应该就是某种需求下的产物。

在开发项目的过程中你会发现有很多页面的布局都是类似的,就比如说常见的设置页面,基本上都是图标+文字+图标的格式,而且出现的频率都很高,如果不做处理势必会写很多无用的代码降低开发效率,正因为如此我们为什么不把这一类view统一封装起来呐(其实就是程序猿的懒惰,哈哈),有了这个想法就开始调研市场上主流应用的显示样式,经过调研总结出一套显示方案涵盖了市面上90%以上的布局样式,目的是在以后的使用中快速高效的开发。

4.2、代码封装

在介绍封装之前先说一句题外话,其实任何功能的封装并不难,还是看你愿不愿意花时间去做,即使是简单的功能真正实现起来的时候还是会或多或少的遇到一些问题,发现问题解决问题的过程其实就是进步的过程。好了,废话不多说,下边看代码。

首先SuperTextView 是继承自 RelativeLayout实现,里边所有布局的添加都是通过Java代码实现的,事先定义好需要的参数名及类型

下边只列出部分参数

    private int backgroundColor;//背景颜色
    private int leftTVColor;//左边文字颜色
    private int leftTopTVColor;//左上文字颜色
    private int leftBottomTVColor;//左下文字颜色
    private int leftBottomTVColor2;//左下第二个文字颜色
    private int rightTVColor;//右边文字颜色
    private int centerTVColor;//中间文字颜色

    private boolean isSingLines = true;//是否单行显示   默认单行
    private int maxLines = 1;//最多几行    默认显示一行
    private int maxEms = 10;//最多几个字    默认显示10个汉子

    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;

然后就是在构造方法里边去实现各个方法

 public SuperTextView(Context context, AttributeSet attrs) {        
        super(context, attrs);
        mContext = context;
        getAttr(attrs);
        initLayout();
    }

我们知道系统控件可以从XML中拿到定义好的属性值,那么我们其实也是可以的,首先在attr中定义好相关属性名及字段类型然后通过AttributeSet去拿到相关属性值在进行设置

private void getAttr(AttributeSet attrs) {        //获取我们在attr中定义的SuperTextView相关属性
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.SuperTextView);        //下边展示的各个类型参数的获取方法,drawable、string、boolean、int、px
        leftIconRes = typedArray.getDrawable(R.styleable.SuperTextView_sLeftIconRes);
        leftTextString = typedArray.getString(R.styleable.SuperTextView_sLeftTextString);
        showCheckBox = typedArray.getBoolean(R.styleable.SuperTextView_sRightCheckBoxShow, false);
        lineType = typedArray.getInt(R.styleable.SuperTextView_sLineShow, DEFAULT);
        topLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sTopLineMargin, defaultLinePadding);        //获取完之后记得recycle()释放掉资源
        typedArray.recycle();
      }

接下来就拿一个view为例进行说明如何动态添加view及设置相关属性

/**
* 初始化左边文字
*/
    private void initLeftText() {
        leftTV = new TextView(mContext);
        leftTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftTextParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
        leftTextParams.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftIconId);
        setMargin(leftTextParams, leftTVMarginLeft, 0, dip2px(mContext, 10), 0);
        leftTV.setId(R.id.sLeftTextId);
        leftTV.setLayoutParams(leftTextParams);
        leftTV.setText(leftTextString);        //设置Params的方法抽离出来便于以后统一调用
        setTextViewParams(leftTV, isSingLines, maxLines, maxEms);        //设置字体颜色的方法抽离出来便于以后统一调用
        setTextColor(leftTV, leftTVColor);        //设置字体大小的方法抽离出来便于以后统一调用
        setTextSize(leftTV, leftTVSize);        //调用addView方法把我们动态创建的view添加到布局中
        addView(leftTV);
    }

初始化完view之后就是要暴露一些方法供外部人员使用了(部分代码示例)
在这里我们返回SuperTextView对象是为了实现链式调用

    /**
     * 获取checkbox状态
     * @return 返回选择框当前选中状态
     */
    
public boolean getCbisChecked() {
    boolean isChecked = false;
    if (rightCheckBox != null) {
        isChecked = rightCheckBox.isChecked();
    }
    return isChecked;
}
  /**
     * 设置左边文字的颜色
     * @param textColor 文字颜色值
     * @return 返回对象
     */
    
public SuperTextView setLeftTVColor(int textColor) {
    leftTVColor = textColor;
    if (leftTV == null) {
        initLeftText();
    } else {
        leftTV.setTextColor(textColor);
    }
    return this;
}

最后就是一些回调方法的使用,以点击事件为例

/**
     * 点击事件接口
     * 这里没有使用interface是因为有些方法我们不是一定要实现的
     * 用到哪个方法再去重写方法就可以了
     */
    
public static class OnSuperTextViewClickListener {
    public void onSuperTextViewClick() {
    }

    public void onLeftTopClick() {
    }

    public void onLeftBottomClick() {
    }

    public void onLeftBottomClick2() {
    }
}

相关的set方法拿到OnSuperTextViewClickListener对象

  
public SuperTextView setOnSuperTextViewClickListener(OnSuperTextViewClickListener listener) {
    onSuperTextViewClickListener = listener;
    return this;
}

初始化view的时候在各个view的点击事件中添加相应的回调方法就行了

       
if(mLeftTopViewIsClickable)

{
    leftTopTV.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (onSuperTextViewClickListener != null) {
                onSuperTextViewClickListener.onLeftTopClick();
            }
        }
    });
}

以上只是源码中的部分方法拿出来供分析使用,看了之后是不是感觉实现其实很简单,赶紧自己着手实现一个吧!

4.3、总结

想必看到这里的小伙伴已经对这个库的源码实现方式了解了,这里只是把实现的流程给大家介绍了一下,我又对这个库做了一个简化版的处理,有兴趣的小伙伴可以去读读源码哦,发现哪里写的不妥的地方还请指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个功能强大的TextView,可以满足日常大部分布局方式,开发者可已自行组合属性配置出属于自己风格的样式!     基本使用1.添加Gradle依赖    dependencies {     ...     compile 'com.allen.supertextview:supertextview:1.0.1'     }2.布局中如何使用    <com.allen.supertextviewlibrary.SuperTextView             android:id="@ id/super_tv"             android:layout_width="match_parent"             android:layout_height="80dp"             stv:sLeftBottomTextColor2="@color/colorAccent"             stv:sLeftBottomTextString="招商银行(8888)"             stv:sLeftBottomTextString2="限额说明>>"             stv:sLeftIconRes="@drawable/bank_zhao_shang"             stv:sLeftTopTextString="银行卡支付"             stv:sRightCheckBoxRes="@drawable/circular_check_bg"             stv:sRightCheckBoxShow="true"             stv:sLineShow="bottom"              />     注意:             1、上下的线可以通过   sLineShow 设置  有四种显示方式 none,top,bottom,both             2、通过设置 sUseRipple=true 开启水波效果3.代码中如何使用   /**  * 可以通过链式设置大部分常用的属性值  */     superTextView.setLeftIcon(drawable)             .setLeftString("")             .setLeftTVColor(0)             .setLeftTopString("")             .setLeftTopTVColor(0)             .setLeftBottomString("")             .setLeftBottomTVColor(0)             .setLeftBottomString2("")             .setLeftBottomTVColor2(0)             .setRightString("")             .setRightTVColor(0)             .setCbChecked(true)             .setCbBackground(drawable)             .setRightIcon(drawable)             .setRightString("")             .setRightTVColor(0)             .setLeftString("")             .setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {                 @Override                 public void onSuperTextViewClick() {                     super.onSuperTextViewClick();                     //do something                 }                 @Override                 public void onLeftTopClick() {                     super.onLeftTopClick();                     //do something                 }                 @Override                 public void onLeftBottomClick() {                     super.onLeftBottomClick();                     //do something                 }                 @Override                 public void onLeftBottomClick2() {                     super.onLeftBottomClick2();                     //do something                 }             });4.点击事件(可根据需求选择实现某个点击事件)    superTextView.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {                 @Override                 public void onSuperTextViewClick() {                     super.onSuperTextViewClick();                     //do something                 }                 @Override                 public void onLeftTopClick() {                     super.onLeftTopClick();                     //do something                 }                 @Override                 public void onLeftBottomClick() {                     super.onLeftBottomClick();                     //do something                 }                 @Override                 public void onLeftBottomClick2() {                     super.onLeftBottomClick2();                     //do something                 }             });5.属性说明(以下属性全部可以通过xml文件配置和代码进行设置)    <declare-styleable name="SuperTextView">     <attr name="sLeftIconRes" format="reference"/>     <attr name="sRightIconRes" format="reference"/>     <attr name="sRightCheckBoxRes" format="reference"/>     <attr name="sLeftTextString" format="string"/>     <attr name="sCenterTextString" format="string"/>     <attr name="sRightTextString" format="string"/>     <attr name="sLeftTopTextString" format="string"/>     <attr name="sLeftBottomTextString" format="string"/>     <attr name="sLeftBottomTextString2" format="string"/>     <attr name="sTopLineMargin" format="dimension"/>     <attr name="sBottomLineMargin" format="dimension"/>     <attr name="sBothLineMargin" format="dimension"/>     <attr name="sLeftIconMarginLeft" format="dimension"/>     <attr name="sLeftTextMarginLeft" format="dimension"/>     <attr name="sLeftTopTextMarginLeft" format="dimension"/>     <attr name="sLeftBottomTextMarginLeft" format="dimension"/>     <attr name="sLeftBottomTextMarginLeft2" format="dimension"/>     <attr name="sRightIconMarginRight" format="dimension"/>     <attr name="sRightTextMarginRight" format="dimension"/>     <attr name="sRightCheckBoxMarginRight" format="dimension"/>     <attr name="sRightCheckBoxShow" format="boolean"/>     <attr name="sIsChecked" format="boolean"/>     <attr name="sUseRipple" format="boolean"/>     <attr name="sLeftTextSize" format="dimension"/>     <attr name="sLeftTopTextSize" format="dimension"/>     <attr name="sLeftBottomTextSize" format="dimension"/>     <attr name="sLeftBottomTextSize2" format="dimension"/>     <attr name="sRightTextSize" format="dimension"/>     <attr name="sCenterTextSize" format="dimension"/>     <attr name="sBackgroundColor" format="color"/>     <attr name="sLeftTextColor" format="color"/>     <attr name="sLeftTopTextColor" format="color"/>     <attr name="sLeftBottomTextColor" format="color"/>     <attr name="sLeftBottomTextColor2" format="color"/>     <attr name="sRightTextColor" format="color"/>     <attr name="sCenterTextColor" format="color"/>     <attr name="sLineShow" format="enum">         <enum name="none" value="0"/>         <enum name="top" value="1"/>         <enum name="bottom" value="2"/>         <enum name="both" value="3"/>     </attr> </declare-styleable>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值