2017-05-11 13 views
2

我有一個自定義開關。我希望文字(開/關)在曲目中,而不是在大拇指中。Android中的文本跟蹤自定義開關

我在考慮設置一個選擇器,其中的文本是可繪製的,並將其設置爲開關的軌跡。但是我無法設置文字的形狀。任何想法我可以做到這一點?

我也想添加一些填充的軌道,我不想讓拇指觸摸軌道。 這是怎麼樣的我多麼希望它看起來:

I want the switch to be kind of like this

這是我的軌跡的形狀:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" 
android:visible="true" 
android:useLevel="false"> 
<corners 
    android:radius="20dp" /> 
<size 
    android:width="48dp" 
    android:height="21dp" /> 
<stroke 
    android:color="#bfbfbf" 
    android:width="6dp"/> 

回答

2

關掉屏幕截圖

enter image description here

上截圖開關

enter image description here

接通圖像

enter image description here

切斷圖像

enter image description here

拇指圖像

enter image description here

switch_on_off.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/on_switch"/> 
    <item android:state_checked="false" android:drawable="@drawable/off_switch"/> 
    <item android:drawable="@drawable/off_switch"/> 
</selector> 

XML代碼

<android.support.v7.widget.SwitchCompat 
    android:id="@+id/switch_compat" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="20dp" 
    app:track="@drawable/switch_on_off" 
    android:thumb="@drawable/on_switch_active"/> 

試試上面的代碼,讓我知道如果你發現任何錯誤.....

+0

這是可能的,但我沒有具體的圖像爲我的設計。我將設計繪製成沒有文字。我需要的只是添加文本。 –

+0

那麼AFAIK,設置文本軌道是不可能的,請參考此鏈接,可能是它可以整理你的查詢: - http://stackoverflow.com/questions/34765660/custom-android-switch-track-animation – Bhavnik