SuperTextView 一款强大的android TextView

来源:互联网 发布:淘宝怎么追加好评 编辑:程序博客网 时间:2024/06/06 02:04

1、功能描述

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:1.1.2'        }

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顶部横线的MarginsTopLineMarginLeftdimension顶部横线的MarginLeftsTopLineMarginRightdimension顶部横线的MarginRightsBottomLineMargindimension底部横线的MarginsBottomLineMarginLeftdimension底部横线的MarginLeftsBottomLineMarginRightdimension底部横线的MarginRightsBothLineMargindimension上下两条横线的MarginsBothLineMarginLeftdimension上下两条横线的MarginLeftsBothLineMarginRightdimension上下两条横线的MarginRightsCenterSpaceHeightdimensiontop和bottom文字间的高度sLeftIconMarginLeftdimension左边图标MarginLeftsLeftTextMarginLeftdimension左边文字MarginLeftsLeftTopTextMarginLeftdimension左上文字MarginLeftsLeftBottomTextMarginLeftdimension左下文字MarginLeftsLeftBottomTextMarginLeft2dimension左下第二个文字MarginLeftsRightTextStringRightIconResreference右边文字的右边图标资源sRightTextStringRightIconResPaddingdimension右边文字的右边图标的PaddingsRightIconMarginRightdimension右边icon的MarginRightsRightTextMarginRightdimension右边文字的MarginRightsRightCheckBoxMarginRightdimension右边CheckBox的MarginRightsRightCheckBoxShowboolean右边的CheckBox是否显示sIsCheckedboolean是否选中sUseRippleboolean是否使用点击出现波纹效果sLeftTextSizedimension左边文字的字体大小sLeftTopTextSizedimension左上文字的字体大小sLeftBottomTextSizedimension左下文字的字体大小sLeftBottomTextSize2dimension左下第二个文字的字体大小sRightTextSizedimension右边文字的字体大小sCenterTextSizedimension中间文字的字体大小sBackgroundColorcolor背景颜色sLeftTextColorcolor左边文字颜色sLeftTopTextColorcolor左上文字颜色sLeftBottomTextColorcolor左下文字颜色sLeftBottomTextColor2color左下第二个文字颜色sRightTextColorcolor右边文字颜色sCenterTextColorcolor中间文字颜色sIsSingLinesboolean是否单行显示sMaxLinesinteger允许的最大行数sMaxEmsinteger允许的最多字数sLineShowenum分割线的显示方式none、top、bottom、bothsBothLineWidthdimension两条分割线的宽度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、总结

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

代码地址:https://github.com/lygttpod/SuperTextView