按鈕應該是咱們的App裏面最廣泛的組件之一了,特別經常使用。
一般咱們寫一個按鈕的套路很簡單也很固定。大概分爲如下幾個步驟:android
這樣一個很標準的按鈕就誕生了,而後就能夠繼續愉快的開發了。這原本沒有什麼問題,也比讓UI妹紙切圖高級了不少,可是隨着開發的進行你會發現,UI妹紙的想法不少,不一樣的界面有各類不一樣圓角和不一樣背景顏色的按鈕,這個時候你須要把上面的三個步驟再進行N次。git
最後你會發現你的drawable目錄下有各類各樣的按鈕背景資源,難以管理。特別是假若有的按鈕要求有點擊效果時須要使用selector,這個時候可能就會產生三個文件用來知足需求,因此總得來講很繁瑣。github
基於以上緣由以及按鈕使用的廣泛程度,感受頗有必要寫一個使用簡單且能知足平常各類需求的按鈕。咱們先梳理下按鈕須要達到的效果:bash
implementation 'top.androidman:superbutton:1.0.1'
複製代碼
Github傳送門app
基本使用 | 單獨設置每一個圓角 | Selector | 圓形按鈕 |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
漸變背景的按鈕 | 有邊框按鈕 | 按鈕不可點擊 | 帶圖標按鈕 |
![]() |
![]() |
![]() |
![]() |
<top.androidman.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
app:color_normal="@color/color_accent"
app:corner="10dp"
app:text="@string/poetry_1"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
<top.androidman.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
app:color_normal="@color/color_6596ff"
app:corner="40dp"
app:corner_left_bottom="0dp"
app:text="單獨設置左下角爲0dp"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
注意:單獨設置角度會覆蓋corner屬性maven
<top.androidman.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
android:onClick="test"
app:color_normal="@color/color_accent"
app:color_pressed="@color/color_ffB419"
app:corner="10dp"
app:text="點擊會變色哦"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
<top.androidman.SuperButton
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_margin="20dp"
android:onClick="test"
app:color_normal="@color/color_accent"
app:drawable_middle="@mipmap/icon_like"
app:drawable_middle_height="50dp"
app:drawable_middle_width="50dp"
app:shape="CIRCLE" />
複製代碼
注意:當設置此屬性時,文字的相關屬性將會失效佈局
<top.androidman.superbutton.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
app:color_direction="RIGHT_LEFT"
app:color_end="@color/color_14c7de"
app:color_start="@color/color_red"
app:corner="20dp"
app:text="從右到左漸變"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
注意:當設置顏色漸變時,color_normal,color_pressed設置將失效ui
<top.androidman.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
app:border_color="@color/color_red"
app:border_width="2dp"
app:color_normal="@color/color_accent"
app:corner="10dp"
app:text="@string/poetry_1"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
<top.androidman.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
android:onClick="test"
app:button_click_able="false"
app:color_normal="@color/color_accent"
app:corner="10dp"
app:singleLine="false"
app:text="app:button_click_able=false\n也能夠代碼設置"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
注意:按鈕提供有方法對點擊事件進行控制,請參考高級使用裏面相關方法spa
<top.androidman.superbutton.SuperButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
app:color_normal="@color/color_red"
app:corner="20dp"
app:drawable_padding="20dp"
app:drawable_right="@mipmap/icon_like"
app:text="圖標在右邊"
app:textColor="@color/color_white"
app:textSize="22sp" />
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SuperButton">
<!--默認配置-->
<attr name="text" format="reference|string" />
<!--按鈕文字顏色-->
<attr name="textColor" format="reference|color" />
<!--按鈕文字大小-->
<attr name="textSize" format="dimension" />
<!--文字是否單行,默認單行-->
<attr name="singleLine" format="boolean" />
<!--默認背景顏色-->
<attr name="color_normal" format="reference|color" />
<!--按壓時的背景顏色-->
<attr name="color_pressed" format="reference|color" />
<!--圖片在文字左邊-->
<attr name="drawable_left" format="reference|color" />
<!--圖片在文字右邊-->
<attr name="drawable_right" format="reference|color" />
<!--圖片在文字上邊-->
<attr name="drawable_top" format="reference|color" />
<!--圖片在文字下邊-->
<attr name="drawable_bottom" format="reference|color" />
<!--圖片距文字的距離-->
<attr name="drawable_padding" format="dimension" />
<!--圖標根據文字大小自動縮放圖標-->
<attr name="drawable_auto" format="boolean" />
<!--只有圖片的狀況,此時會忽略文字,即使設置-->
<attr name="drawable_middle" format="reference|color" />
<!--圖片在中間時寬-->
<attr name="drawable_middle_width" format="dimension" />
<!--圖片在中間時高-->
<attr name="drawable_middle_height" format="dimension" />
<!--形狀-->
<attr name="shape" format="enum">
<!--圓形-->
<enum name="CIRCLE" value="1" />
<!--矩形-->
<enum name="RECT" value="2" />
</attr>
<!--按鈕背景是漸變色時設置-->
<!--開始顏色-->
<attr name="color_start" format="color" />
<!--結束顏色-->
<attr name="color_end" format="color" />
<!--顏色方向-->
<attr name="color_direction" format="enum">
<!--從上到下-->
<enum name="TOP_BOTTOM" value="0x1" />
<!--從右上到左下-->
<enum name="TR_BL" value="0x2" />
<!--從右到左-->
<enum name="RIGHT_LEFT" value="0x3" />
<!--從右下到左上-->
<enum name="BR_TL" value="0x4" />
<!--從下到上-->
<enum name="BOTTOM_TOP" value="0x5" />
<!--從左下到右上-->
<enum name="BL_TR" value="0x6" />
<!--從左到右-->
<enum name="LEFT_RIGHT" value="0x7" />
<!--從左上到右下-->
<enum name="TL_BR" value="0x8" />
</attr>
<!--按鈕圓角,若是單獨設置會覆蓋此設置-->
<attr name="corner" format="dimension" />
<!--左上角圓角半徑-->
<attr name="corner_left_top" format="dimension" />
<!--左下角圓角半徑-->
<attr name="corner_left_bottom" format="dimension" />
<!--右上角圓角半徑-->
<attr name="corner_right_top" format="dimension" />
<!--右下角圓角半徑-->
<attr name="corner_right_bottom" format="dimension" />
<!--邊框寬度-->
<attr name="border_width" format="dimension" />
<!--邊框顏色-->
<attr name="border_color" format="color" />
<!--按鈕是否能夠點擊-->
<attr name="button_click_able" format="boolean" />
</declare-styleable>
</resources>
複製代碼
1.想修改按鈕相關調用以下方法:設計
/**
* 修改文字
*/
superButton.setText(CharSequence text);
/**
* 修改文字顏色
*/
superButton.setTextColor(@ColorInt int textColor);
/**
* 修改按鈕背景顏色
*/
superButton.setColorNormal(@ColorInt int colorNormal);
複製代碼
當某些狀態下須要代碼控制,將按鈕置灰而後不可點擊,能夠直接調用以下方法:
/**
* 調用此方法後按鈕顏色被改變,按鈕沒法點擊
*/
superButton.setUnableColor(@ColorInt int color);
複製代碼
若是隻是想設置按鈕不可點擊,不須要改變按鈕顏色,能夠使用以下方法
/**
* 設置按鈕是否能夠點擊
*/
superButton.setButtonClickable(boolean buttonClickable);
複製代碼
將來準備把按鈕陰影加入進來,這樣這個按鈕的使用就更加方便啦! 因此若是你們有好的想法和需求,歡迎你們提issue。