1. 程式人生 > >Android_首頁_按鈕切換效果_選中、未選中

Android_首頁_按鈕切換效果_選中、未選中

一、對於首頁介面底部有多個按鈕,如按鈕(1,2,3),預設點選1,按鈕1圖片是選中狀態,當點選2時,按鈕2是選中圖片,然而其它的是未選中狀態,

二、具體操作:

1、畫介面 在activity_main.xml  編寫就好

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/home"
            style="@style/BtnStyle"
            android:button="@null"
            android:drawableTop="@drawable/btn_state"
            android:text="首頁" />

        <RadioButton
            android:id="@+id/home2"
            style="@style/BtnStyle"
            android:button="@null"
            android:drawableTop="@drawable/btn_state"
            android:text="精品" />

        <RadioButton
            android:id="@+id/home3"
            style="@style/BtnStyle"
            android:drawableTop="@drawable/btn_state"
            android:button="@null"
            android:text="我的" />
    </RadioGroup>
2、編寫RadioButton的樣式:

在styles.xml檔案中加入介面優點太醜了,主要是實現的步驟,覺得不好的,就可以自己畫,嘻嘻

    <style name="BtnStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">10dp</item>
    </style>
3、在drawable中,建立btn_state.xml檔案  一個按鈕,要對應一個按鈕狀態改變xml檔案
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/home_active"></item>
	<item android:state_checked="false" android:drawable="@drawable/home_unactive"></item>
</selector>
4、在MainActivity中載入activity_main.xml就可以了