Android自訂控制項皮膚

來源:互聯網
上載者:User

標籤:android   控制項   

Android自訂控制項皮膚

對於Android的內建控制項,其外觀只能說中規中矩,而我們平時所看到的Android應用中,一個簡單的按鈕都做得十分美觀,甚至於許多按鈕在按下時的外觀都有一定變化,使用者體驗十分好。這其中,就涉及到了Android自訂控制項屬性的操作方法,以下操作以實現自訂按鈕皮膚為例。

1. 我們要自訂將要實現的外觀狀態,可以是圖片或者是自訂的xml,這是我們直接自訂不同狀態的顏色xml,在values目錄下建立colors.xml,代碼如下:

colors.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="btn_no_pressed">#3300ffff</color>    <color name="btn_pressed">#33ffff00</color></resources>

檔案中的name可以根據項目需要自訂命名,值是採用argb格式。

2. 我們要對將要實現的按鈕不同狀態進行設定,在drawable目錄下建立btn_bg.xml檔案,類型記得選擇“selector”,代碼如下:

btn_bg.xml

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

android:state_pressed=""定義了控制項是否被按下,值為boolean,類似的屬性大家可以自行嘗試學習,android:drawable=""定義了該控制項處於相應狀態時的外觀。

3. 我們此時就可以將定義好的狀態應用於布局檔案中了,代碼如下:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button         android:id="@+id/mybtn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="click me"        android:background="@drawable/btn_bg"        /></RelativeLayout>

上述步驟做完之後,就可以將應用運行調試了,這隻是一個簡單的例子>~<

Android自訂控制項皮膚

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.