2015-10-27 22 views
0

我正在編寫一個應用程序,將有60個複選框,編號爲1-60。 我的目標是要實現這樣的:Android自定義複選框(或ToggleButton)裏面的數字

Uncheckedenter image description here

有沒有辦法實現這一目標,而不必畫裏面數60個.png文件?我可以只繪製邊緣並添加數字作爲按鈕的文本屬性嗎?

我嘗試定製一個切換按鈕(得到了here的想法)

<ToggleButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:button="@drawable/toggle_selector" 
    android:background="@null" 
    android:paddingLeft="10dp" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:textOn="60" 
    android:textOff="60" /> 

toogle_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_checked="true" 
     android:drawable="@drawable/checked" /> 
    <item 
     android:drawable="@drawable/not_checked" /> 
</selector> 

但作爲一個結果,文本放置在右側按鈕。是否可以將文本放置在圖像的頂部?

+0

也許刪除 「paddingLeft」 項目? – Christine

+0

@Christine,剛剛嘗試過,但沒有任何區別 – Lelo

回答

1

你可以用這樣的切換按鈕頂部的textview相對佈局來做到這一點。更換利潤率和大小根據自己的需要

<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:background="@color/background_floating_material_dark" 
    tools:context="com.contag.app.fragment.NavDrawerFragment"> 
    <ToggleButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/toggle_selector" 
     android:background="@android:color/transparent" 
     android:gravity="center" 
     android:textOn="@null" 
     android:textOff="@null" 
     android:minHeight="0dp" 
     android:minWidth="0dp" 
     android:id="@+id/toggleButton" 
     android:layout_marginTop="26dp" 
     android:layout_marginStart="156dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="60" 
     android:textColor="@android:color/holo_green_dark" 
     android:textSize="15sp" 
     android:id="@+id/textView" 
     android:layout_alignLeft="@+id/toggleButton" 
     android:layout_alignTop="@+id/toggleButton" 
     android:layout_alignRight="@+id/toggleButton" 
     android:layout_alignBottom="@+id/toggleButton" 
     android:gravity="center" /> 
</RelativeLayout> 

Looks like this

+0

聰明的想法..謝謝! – Lelo