在进行Android利用开发过程中,设计师常常会给利用中波及的卡片和按钮来个圆角。对于卡片,咱们能够间接应用CardView等,对于圆角按钮通常会shape、selector等xml的形式进行配置。
尽管shape、selector的xml写法能够解决视觉问题,然而写的很多,对于代码的简洁性来说的确大打折扣,并且xml对于Apk包的大小来说也不是很敌对。所以,咱们无妨思考试试自定义圆角组件的形式来解决问题。
基于按钮的一些罕用的属性,咱们提供了如下的一些属性,比方,按钮的圆角大小、圆角色彩、按钮色彩、文字色彩、独自设置4个角的圆角大小等。
<declare-styleable name="RectgleTextView"> <attr name="shapeType" format="integer|enum"> <enum name="rectangle" value="0" /> <enum name="oval" value="1" /> <enum name="LINE" value="2" /> </attr> <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="topLeft" format="dimension" /> <attr name="topRight" format="dimension" /> <attr name="bottomLeft" format="dimension" /> <attr name="bottomRight" format="dimension" /> <attr name="strColor" format="color" /> <attr name="strWidth" format="dimension" /> <attr name="solidBac" 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" /> <attr name="textLeftStyle"> <enum name="bold" value="1" /> <enum name="italic" value="2" /> </attr> <attr name="textRightStyle"> <enum name="bold" value="1" /> <enum name="italic" value="2" /> </attr> <attr name="textCenterStyle"> <enum name="bold" value="1" /> <enum name="italic" value="2" /> </attr> <attr name="autoMaxHeight" format="boolean" /> <attr name="gradientOrientation"> <enum name="top_bottom" value="0" /> <enum name="tp_bl" value="1" /> <enum name="right_left" value="2" /> <enum name="br_tl" value="3" /> <enum name="bottom_top" value="4" /> <enum name="bl_tr" value="5" /> <enum name="left_right" value="6" /> <enum name="tl_br" value="7" /> </attr> <attr name="startSolid" format="reference|color" /> <attr name="centerSolid" format="reference|color" /> <attr name="endSolid" format="reference|color" /> </declare-styleable>
而后,咱们创立一个自定义的View,RectgleTextView继承自AppCompatTextView。而后,就是对咱们自定义的属性进行解决,具体不再解释,能够看文末的源码。最初,只须要在布局中引入咱们自定义的组件即可,比方。
<com.avatar.demo.shape.RectgleTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_gravity="center" android:paddingHorizontal="15dp" android:paddingVertical="8dp" android:text="实心圆角按钮" android:textColor="@color/white" android:textSize="18sp" app:solidBac="@color/black" app:totalRadius="5dp" />
以下是局部效果图。
附件: 自定义圆角组件