项目中会常常用到横向的图文布局,好比下面这些: android
还有这样的布局,常常出如今设置性的界面:git
这些布局虽然不难,可是长相相似,频繁出现,每次都要手写这么多代码仍是很累的。彻底能够自定义一个布局,兼容这些常见的场景,经过对外暴漏一些属性来设置里面的内容。github
因而我自定义了SuperLayout,实现了常见的场景。使用时只须要写一个布局,设置一些属性便可实现效果,能将原先手写的布局代码减小80%左右,大大提升布局的编写效率。app
在xml中这样使用:布局
<com.lxj.androidktx.widget.SuperLayout android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:paddingLeft="14dp" android:paddingRight="15dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:layout_marginTop="15dp" app:sl_leftImageSrc="@drawable/avatar" app:sl_leftText="头像" app:sl_leftTextColor="#222" app:sl_leftTextSize="18sp" app:sl_rightImageSrc="@mipmap/jt" app:sl_solid="#3EDCE9" app:sl_corner="15dp" app:sl_strokeWidth="2dp" app:sl_stroke="#f00" android:layout_width="match_parent" android:layout_height="wrap_content"/>
复制代码
全部能够设置的属性以下:spa
<declare-styleable name="SuperLayout">
<attr name="sl_leftImageSrc" format="reference"/>
<attr name="sl_leftImageSize" format="dimension"/>
<attr name="sl_leftText" format="string"/>
<attr name="sl_leftTextColor" format="color"/>
<attr name="sl_leftTextSize" format="dimension"/>
<attr name="sl_leftTextMarginLeft" format="dimension"/>
<attr name="sl_leftTextMarginTop" format="dimension"/>
<attr name="sl_leftTextMarginRight" format="dimension"/>
<attr name="sl_leftTextMarginBottom" format="dimension"/>
<attr name="sl_leftSubText" format="string"/>
<attr name="sl_leftSubTextColor" format="color"/>
<attr name="sl_leftSubTextSize" format="dimension"/>
<attr name="sl_centerText" format="string"/>
<attr name="sl_centerTextColor" format="color"/>
<attr name="sl_centerTextGravity" format="integer" />
<attr name="sl_centerTextBg" format="reference"/>
<attr name="sl_centerTextSize" format="dimension"/>
<attr name="sl_rightText" format="string"/>
<attr name="sl_rightTextColor" format="color"/>
<attr name="sl_rightTextSize" format="dimension"/>
<attr name="sl_rightTextBg" format="reference"/>
<attr name="sl_rightTextBgColor" format="color"/>
<attr name="sl_rightTextVerticalPadding" format="dimension"/>
<attr name="sl_rightTextHorizontalPadding" format="dimension"/>
<attr name="sl_rightImageSrc" format="reference"/>
<attr name="sl_rightImageMarginLeft" format="dimension"/>
<attr name="sl_rightImageSize" format="dimension"/>
<attr name="sl_rightImage2Src" format="reference"/>
<attr name="sl_rightImage2MarginLeft" format="dimension"/>
<attr name="sl_rightImage2Size" format="dimension"/>
<attr name="sl_solid" format="color"/>
<attr name="sl_corner" format="dimension"/>
<attr name="sl_stroke" format="color"/>
<attr name="sl_strokeWidth" format="dimension"/>
<attr name="sl_topLineColor" format="color"/>
<attr name="sl_bottomLineColor" format="color"/>
<attr name="sl_enableRipple" format="boolean"/>
<attr name="sl_rippleColor" format="color"/>
</declare-styleable>
复制代码
在使用以前须要先添加依赖,这个类放在了AndroidKTX
类库中,须要依赖一下:code
implementation 'com.lxj:androidktx:1.2.0'
//for androidx
implementation 'com.lxj:androidktx:1.2.0-x'
复制代码
AndroidKTX包含了一些列很是有用的Kotlin扩展和通用控件,代码很少,个个都很实用。若是你用Kotlin开发,必定不要错过。它的地址是:github.com/li-xiaojun/…orm