2.1.2_按鈕_Button

1、StateListDrawable

為按鈕設置背景,且能夠隨按鈕狀態(tài)的不同而改變
  • drawable:引用的Drawable位圖,我們可以把他放到最前面,就表示組件的正常狀態(tài)~
  • state_focused:是否獲得焦點
  • state_window_focused:是否獲得窗口焦點
  • state_enabled:控件是否可用
  • state_checkable:控件可否被勾選,eg:checkbox
  • state_checked:控件是否被勾選
  • state_selected:控件是否被選擇,針對有滾輪的情況
  • state_pressed:控件是否被按下
  • state_active:控件是否處于活動狀態(tài),eg:slidingTab
  • state_single:控件包含多個子控件時,確定是否只顯示一個子控件
  • state_first:控件包含多個子控件時,確定第一個子控件是否處于顯示狀態(tài)
  • state_middle:控件包含多個子控件時,確定中間一個子控件是否處于顯示狀態(tài)
  • state_last:控件包含多個子控件時,確定最后一個子控件是否處于顯示狀態(tài)
  • 案例:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ic_course_bg_fen"/>
    <item android:state_enabled="false" android:drawable="@drawable/ic_course_bg_pressed"/>
    <item android:drawable="@drawable/ic_course_bg_cheng"/>
</selector>

2、ShapeDrawable

為按鈕設置背景色、圓角、邊框等屬性
  • solid:背景色
  • stroke:邊框
  • padding:內(nèi)邊距
  • corners:圓角
  • gradient:漸變色
  • 案例
<shape>
<solid android:color="@color/bbutton_danger_pressed" />
<stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />
<corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
</shape>

3、Tip

StateListDrawable 里面可以引用 ShapeDrawable,而且可以 \color{red}{內(nèi)嵌}
案例:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/bbutton_danger_pressed" />
            <stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />
            <corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
        </shape>
    </item>
        
    <item android:state_enabled="false">
        <shape>
            <solid android:color="@color/bbutton_danger_disabled" />
            <stroke android:width="1dp" android:color="@color/bbutton_danger_disabled_edge" />
            <corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@color/bbutton_danger" />
            <stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />
            <corners android:radius="@dimen/bbuton_rounded_corner_radius"/>
        </shape>
    </item>
</selector>
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容