2017-05-11 1 views
2

私はカスタムスイッチを持っています。テキスト(オン/オフ)をトラックに、親指ではなくしたいです。Androidのカスタムスイッチ(トラックがテキストの場合)

私はdrawableにテキストを持つセレクタを設定し、それをスイッチのトラックとして設定することを考えていました。しかし、私は形にテキストを設定することはできません。どのように私はこれを行うことができます任意のアイデア?

トラックにパディングを追加したい場合は、親指でトラックに触れないでください。

<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

I want the switch to be kind of like this

これは私のトラックの形状である: これは私がそれを見てみたい方法の一種であります

スイッチスイッチ

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

関連する問題