亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Android 自定義RadioButton樣式

original 2016-11-07 11:40:42 595
abstrait:上面這種3選1的效果如何做呢?用代碼寫? 其實(shí)有更簡單的辦法,忘了RadioButton有什么特性了嗎? 我就用RadioButton實(shí)現(xiàn)了如上效果,其實(shí)很簡單的。 首先定義一張background,命名為radio.xml,注意該background必須為xml樣式的圖片: <?xml version="1.0"&nbs

21.png

上面這種3選1的效果如何做呢?用代碼寫? 
其實(shí)有更簡單的辦法,忘了RadioButton有什么特性了嗎? 
我就用RadioButton實(shí)現(xiàn)了如上效果,其實(shí)很簡單的。 
首先定義一張background,命名為radio.xml,注意該background必須為xml樣式的圖片: 

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

這里我們只要關(guān)心state_checked狀態(tài)就可以了。所以很簡單的配置。 
接下來就是布局文件里面引用這張圖片了:

<RadioGroup  
        android:gravity="center"  
        android:orientation="horizontal"    
        android:layout_width="fill_parent"     
        android:layout_height="wrap_content"  
        >  
        <RadioButton     
            android:id="@+id/btn_0"    
            android:layout_width="fill_parent"     
            android:layout_height="wrap_content"  
            android:text="最新信息"   
            android:textSize="17.0sp"     
            android:textColor="@android:color/black"    
            android:gravity="center"   
            android:layout_weight="1"  
            android:checked="true"  
            android:button="@null"  
            android:background="@drawable/radio"  
            />    
        <RadioButton     
            android:id="@+id/btn_1"  
            android:layout_width="fill_parent"     
            android:layout_height="wrap_content"  
            android:text="在線專家"  
            android:textSize="17.0sp"  
            android:textColor="@android:color/black"    
            android:gravity="center"  
            android:layout_weight="1"  
            android:button="@null"  
            android:background="@drawable/radio"  
            />    
        <RadioButton     
            android:id="@+id/btn_2"  
            android:layout_width="fill_parent"     
            android:layout_height="wrap_content"  
            android:text="預(yù)約服務(wù)"  
            android:textSize="17.0sp"  
            android:textColor="@android:color/black"    
            android:gravity="center"  
            android:layout_weight="1"  
            android:button="@null"  
            android:background="@drawable/radio"  
            />    
    </RadioGroup>

注意將android:button="@null"全部設(shè)為null,并且將android:background="@drawable/radio"設(shè)為剛才新建的圖片。這樣就完成了。 
一句代碼都不用寫! 
看看豎下來的效果: 
 22.png

旁邊有圖標(biāo)的怎么辦? 
 23.png

也很簡單啦,只要在每個RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。 
另外要設(shè)置圖標(biāo)與文字的距離怎么辦? 
有一個方法setCompoundDrawablePadding(pad)可以設(shè)置圖標(biāo)與文字的距離,對應(yīng)的屬性為android:drawablePadding。 
有了這招完全可以實(shí)現(xiàn)類似的n選1效果,看下面: 

24.png 

25.png 


Notes de version

Entrées populaires