項目中會常常用到橫向的圖文佈局,好比下面這些: 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