自定义一个android通用shape样式的TextView

在开发中经常遇到需要各种样式的shape定义来实现一些圆角、渐变等UI效果。所以每一次都需要自己去定义,然而这个过程会产生很多问题,比如命名规范就是一个很头疼的问题。而且文件多了以后也会增加包体积。可复用的概率又很低。所以需要一个通用样式的shape。今天他来了。。。

首先,在values中定义一个xml:shape_asster.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <declare-styleable name="IconFontView">
            <attr name="fontAsset" format="string"/>
            <attr name="android:textColor"/>
            <attr name="android:text"/>
        </declare-styleable>

        <declare-styleable name="CheckableImageButton">
            <attr name="android:checked"/>
        </declare-styleable>

        <declare-styleable name="ShadowCardView">
            <attr name="android:background"/>
            <attr name="android:padding"/>
            <attr name="android:shadowColor"/>
            <attr name="shadow_radius" format="dimension"/>
        </declare-styleable>

        <declare-styleable name="CommonShapeView">
            <!--shape模式-->
            <attr name="cs_shapeMode" format="enum">
                <enum name="rectangle" value="0"/>
                <enum name="oval" value="1"/>
                <enum name="line" value="2"/>
                <enum name="ring" value="3"/>
            </attr>
            <!--填充颜色-->
            <attr name="cs_fillColor" format="color"/>
            <!--按压颜色-->
            <attr name="cs_pressedColor" format="color"/>
            <!--描边颜色-->
            <attr name="cs_strokeColor" format="color"/>
            <!--描边宽度-->
            <attr name="cs_strokeWidth" format="dimension"/>
            <!--圆角大小-->
            <attr name="cs_cornerRadius" format="dimension"/>
            <!--圆角位置-->
            <attr name="cs_cornerPosition">
                <flag name="topLeft" value="1"/>
                <flag name="topRight" value="2"/>
                <flag name="bottomRight" value="4"/>
                <flag name="bottomLeft" value="8"/>
            </attr>
            <!--是否开启动效-->
            <attr name="cs_activeEnable" format="boolean"/>
            <!--渐变开始颜色-->
            <attr name="cs_startColor" format="color"/>
            <!--渐变结束颜色-->
            <attr name="cs_endColor" format="color"/>
            <!--渐变方向-->
            <attr name="cs_orientation" format="enum">
                <enum name="TOP_BOTTOM" value="0"/>
                <enum name="LEFT_RIGHT" value="1"/>
            </attr>
        </declare-styleable>

        <declare-styleable name="FitWindowLayout">
            <attr name="fitStatusBar" format="boolean"/>
        </declare-styleable>

        <!--边沿透明View-->
        <declare-styleable name="EdgeTransparentView">
            <attr name="edge_position">
                <flag name="top" value="0x01"/>
                <flag name="bottom" value="0x02"/>
                <flag name="left" value="0x04"/>
                <flag name="right" value="0x08"/>
            </attr>

            <attr name="edge_width" format="dimension"/>
        </declare-styleable>

        <declare-styleable name="MarqueeTextView">
            <attr name="mtv_scrollInterval" format="integer" />
            <attr name="mtv_scrollFirstDelay" format="integer" />
            <attr name="mtv_scrollMode">
                <enum name="mode_forever" value="100" />
                <enum name="mode_once" value="101" />
            </attr>
        </declare-styleable>
</resources>

在添加完xml后,新建一个类文件:CommonShapeTextView.kt

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
class CommonShapeTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
     val shapeUtil = CommonShapeUtil(this, context, attrs)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        shapeUtil.onMeasure()
    }
}

至此。前期的集成工作就已完成。下面来说一下用法:

<com.changjiu56.wms.view.CommonShapeTextView
        android:id="@+id/tv_login"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_48"
        android:foreground="?android:attr/selectableItemBackground"
        android:gravity="center"
        android:text="@string/text_login_button"
        android:textColor="@color/white"
        android:textSize="@dimen/sp_18"
        android:textStyle="bold"
        app:cs_cornerRadius="@dimen/dp_9"  此为圆角设置
        app:cs_fillColor="@color/app_essentialColor" 此为填充颜色
        app:layout_constraintTop_toBottomOf="@id/ed_pwd" />

具体的属性用法。可以阅读shape_asster.xml。用法讲解到此结束。
END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值