自訂控制項(四)自訂點擊效果Button,控制項button

來源:互聯網
上載者:User

自訂控制項(四)自訂點擊效果Button,控制項button

本節自訂帶有點擊效果的圓角矩形Button。
功能
1.button點擊變色
2.上面文字也是點擊變色
3.button形狀為圓角矩形
4.button具有漸層效果

主要分為五步
首先你需要在res目錄下建立一個drawable目錄(與像素無關的drawable)。
1.設定按鈕正常狀態

//button_shape_normal.xml<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">  <!--  設定為矩形-->    <corners android:radius="3dp"/> <!--  設定圓角矩形半徑-->    <gradient         android:angle="90"        android:endColor="#cccccc"        android:startColor="#acacac"/>  <!--  指定色彩漸層(起始顏色,終止顏色,漸層方向)--></shape>

2.設定按鈕點擊狀態

//button_shape_pressed.xml<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">        <corners android:radius="3dp"/> <!--  設定圓角矩形半徑-->    <gradient         android:angle="270"        android:endColor="#cccccc"        android:startColor="#acacac"/>  <!--  指定色彩漸層(起始顏色,終止顏色,漸層方向)--></shape>

3.設定Button的selector

//button_shape_selector.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/button_shape_normal"        android:state_pressed="false"/>    <item android:drawable="@drawable/button_shape_pressed"        android:state_pressed="true"/></selector>

4.設定文字顏色selector

//button_text_color.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_pressed="false" android:color="#ffffff"/>    <item android:state_pressed="true" android:color="#556699"/></selector>

5.在button屬性中引用剛剛建立的資源

<Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="wangjiang"        android:textSize="16sp"        android:background="@drawable/button_shape_selector"         android:textColor="@drawable/button_text_color"/>

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.