Wie benutzerdefinierte Schaltertaste?

Lesezeit: 11 Minuten

Wie benutzerdefinierte Schaltertaste
Cool

Ich suche nach Custom The Switch Schaltfläche zum Werden wie folgt:

Geben Sie hier die Bildbeschreibung ein

Wie erreicht man das?

Wie benutzerdefinierte Schaltertaste
Sanjet A

Allerdings wähle ich vielleicht nicht den besten Ansatz, aber so habe ich einige erstellt Switch wie UIs in einigen meiner Apps. Hier ist der Code –

<RadioGroup
        android:checkedButton="@+id/offer"
        android:id="@+id/toggle"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="@dimen/margin_medium"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="@dimen/margin_medium"
        android:background="@drawable/pink_out_line"
        android:orientation="horizontal">

        <RadioButton
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_marginLeft="1dp"
            android:id="@+id/search"
            android:background="@drawable/toggle_widget_background"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="Search"
            android:textColor="@color/white" />

        <RadioButton
            android:layout_marginRight="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:id="@+id/offer"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/toggle_widget_background"
            android:button="@null"
            android:gravity="center"
            android:text="Offers"
            android:textColor="@color/white" />
    </RadioGroup>

pink_out_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="2dp" />
    <solid android:color="#80000000" />
    <stroke
        android:width="1dp"
        android:color="@color/pink" />
</shape>

toggle_widget_background.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/pink" android:state_checked="true" />
    <item android:drawable="@color/dark_pink" android:state_pressed="true" />
    <item android:drawable="@color/transparent" />
</selector>

Und hier ist die Ausgabe –Geben Sie hier die Bildbeschreibung ein

  • Das hat fantastisch funktioniert. Die einzige Änderung, die ich auch vorgenommen habe, war das Hinzufügen von android:textColor zum Selektor und das Anwenden dieser auf den Text der Optionsfelder. Dann könnte ich weißen Text auf dem ausgewählten Element und schwarzen Text auf dem nicht ausgewählten Element haben.

    – runfaj

    21. Mai 16 um 23:29 Uhr

  • Ich habe android:textColor wie unten hinzugefügt und android:textColor=”@color/white” aus dem Radio entfernt Schaltfläche, aber die Textfarbe der Optionsschaltflächen ändert sich nicht

    – Prashanth Debbadwar

    14. Juni 16 um 10:56 Uhr


  • Ich denke, Sie sollten “android:state_checked” verwenden

    – Sanjet A

    14. Juni 16 um 11:26 Uhr


  • Das ist großartig. Ich war verwirrt zwischen Toggle-Button und Switch-Button und Radio-Button. Und nachdem ich jetzt Ihre Lösung erhalten habe, werde ich mit dem Optionsfeld bestätigt.

    – Jigar

    21. Juli 16 um 9:26 Uhr


  • @SanjeetAjnabee Vielen Dank

    – mahdi pishguy

    5. Oktober 16 um 8:32 Uhr

1643907788 909 Wie benutzerdefinierte Schaltertaste
Prashant Arvind

Es ist ein einfaches XML-Design. Es sieht aus wie ein iOS-Schalter, überprüfen Sie das folgende Bild

Geben Sie hier die Bildbeschreibung ein

Sie müssen custom_thumb.xml und custom_track.xml erstellen

Dies ist mein Schalter, ich brauche einen sehr großen Schalter, also habe ich den Parameter layout_width/layout_height hinzugefügt

 <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/swOnOff"
        android:layout_width="@dimen/_200sdp"
        android:layout_marginStart="@dimen/_50sdp"
        android:layout_marginEnd="@dimen/_50sdp"
        android:layout_marginTop="@dimen/_30sdp"
        android:layout_gravity="center"
        app:showText="true"
        android:textSize="@dimen/_20ssp"
        android:fontFamily="@font/opensans_bold"
        app:track="@drawable/custom_track"
        android:thumb="@drawable/custom_thumb"
        android:layout_height="@dimen/_120sdp"/>

Erstellen Sie nun die Datei custom_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="oval">
            <solid android:color="#ffffff"/>
            <size android:width="@dimen/_100sdp"
                android:height="@dimen/_100sdp"/>
            <stroke android:width="1dp"
                android:color="#8c8c8c"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="oval">
            <solid android:color="#ffffff"/>
            <size android:width="@dimen/_100sdp"
                android:height="@dimen/_100sdp"/>
            <stroke android:width="1dp"
                android:color="#34c759"/>
        </shape>
    </item>
</selector>

Erstellen Sie nun die Datei custom_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <corners android:radius="@dimen/_100sdp" />
            <solid android:color="#ffffff" />
            <stroke android:color="#8c8c8c" android:width="1dp"/>
            <size android:height="20dp" />
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <corners android:radius="@dimen/_100sdp" />
            <solid android:color="#34c759" />
            <stroke android:color="#8c8c8c" android:width="1dp"/>
            <size android:height="20dp" />
        </shape>
    </item>
</selector>

  • Perfekt! Klappt wunderbar.

    – Geschrei

    13. Januar 20 um 13:36 Uhr

  • Könnten Sie Ihre Dimen-Datei bereitstellen?

    – Arpit Patel

    20. November 2020 um 15:37 Uhr

  • @ArpitPatel, dass die Dimen-Datei aus dieser Abhängigkeit stammt, fügen Sie dies in Ihre Gradle-Datei ein und boomen Sie, dieser Fehler wird behoben sein —– kopieren Sie ihn einfach * fügen Sie ihn ein Implementierung „com.intuit.sdp:sdp-android:1.0.6“

    – Nik

    18. Dezember 2020 um 22:55 Uhr


1643907789 304 Wie benutzerdefinierte Schaltertaste
sagte

Sie können den folgenden Code zum Ändern verwenden Farbe und Text :

<org.jraf.android.backport.switchwidget.Switch
                        android:id="@+id/th"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:thumb="@drawable/apptheme_switch_inner_holo_light"
                        app:track="@drawable/apptheme_switch_track_holo_light"
                        app:textOn="@string/switch_yes"
                        app:textOff="@string/switch_no"
                        android:textColor="#000000"
                        />

Erstellen Sie eine XML-Datei mit dem Namen colors.xml im Ordner res/values:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
</resources>

Erstellen Sie im Drawable-Ordner eine XML-Datei my_btn_toggle.xml:

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@color/red"  />
        <item android:state_checked="true" android:drawable="@color/green"  />
    </selector>

und im XML-Abschnitt definieren Sie Ihre Toggle-Schaltfläche hinzufügen:

android:background="@drawable/my_btn_toggle

um die Farbe zu ändern textOn und textOffbenutzen

android:switchTextAppearance="@style/Switch"

  • Übrigens, wenn jemand Probleme mit XML-Fehlern hat (ungebundenes Tag-Präfix), stellen Sie sicher, dass Sie hinzufügen xmlns:app="http://schemas.android.com/apk/res-auto" an den Anfang des Layoutblatts. Damit können Sie die verwenden app: Präfix.

    – Singular1ty

    9. August 14 um 0:59 Uhr

  • schnelle Annäherung!! aber ich möchte hier ein paar Punkte hinzufügen, wenn Sie auf diese Weise Thumb- und Track-Attribute in XML auf @null setzen.

    – Ankur Chaudhary

    12. Mai 15 um 5:13 Uhr

1643907790 570 Wie benutzerdefinierte Schaltertaste
SiavA

<Switch android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:thumb="@drawable/custom_switch_inner_holo_light"
                    android:track="@drawable/custom_switch_track_holo_light"
                    android:textOn="@string/yes"
                    android:textOff="@string/no"/>

drawable/custom_switch_inner_holo_light.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/custom_switch_thumb_disabled_holo_light" />
    <item android:state_pressed="true"  android:drawable="@drawable/custom_switch_thumb_pressed_holo_light" />
    <item android:state_checked="true"  android:drawable="@drawable/custom_switch_thumb_activated_holo_light" />
    <item                               android:drawable="@drawable/custom_switch_thumb_holo_light" />
</selector>

drawable/custom_switch_track_holo_light.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"  android:drawable="@drawable/custom_switch_bg_focused_holo_light" />
    <item                               android:drawable="@drawable/custom_switch_bg_holo_light" />
</selector>

Die nächsten Bilder sind 9.paths Drawables und sie müssen in unterschiedlicher Dichte vorliegen (mdpi, hdpi, xhdpi, xxhdpi). Als Beispiel gebe ich xxhdpi (Sie können die Größe bei Bedarf ändern):

drawable/custom_switch_thumb_disabled_holo_light

custom_switch_thumb_disabled_holo_light

drawable/custom_switch_thumb_pressed_holo_light

custom_switch_thumb_pressed_holo_light

drawable/custom_switch_thumb_activated_holo_light

custom_switch_thumb_activated_holo_light

drawable/custom_switch_thumb_holo_light

custom_switch_thumb_holo_light

drawable/custom_switch_bg_focused_holo_light

custom_switch_bg_focused_holo_light

drawable/custom_switch_bg_holo_light

Geben Sie hier die Bildbeschreibung ein

Dies habe ich erreicht

Geben Sie hier die Bildbeschreibung ein

dadurch:

1) benutzerdefinierter Selektor:

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

2) mit v7 SwitchCompat

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:button="@drawable/checkbox_yura"
    android:thumb="@null"
    app:track="@null"/>

  • Dadurch wird die Schaltfläche zum Anfang der Ansicht. Was ist, wenn die Schaltfläche am Ende des Schalters platziert werden muss (nach dem Text des Schalters)

    – Tarun Kumar

    21. Juni 19 um 10:06 Uhr

  • @TarunKumar hast du etwas zum Ausrichten des Layouts in der Mitte?

    – Parth Patel

    13. Mai 20 um 7:50 Uhr

1643907792 790 Wie benutzerdefinierte Schaltertaste
Löwenschreiber

Ich verwende diesen Ansatz, um einen benutzerdefinierten Schalter mit a zu erstellen RadioGroup und RadioButton;

Vorschau

Geben Sie hier die Bildbeschreibung ein

Farbressource

<color name="blue">#FF005a9c</color>
<color name="lightBlue">#ff6691c4</color>
<color name="lighterBlue">#ffcdd8ec</color>
<color name="controlBackground">#ffffffff</color>

control_switch_color_selector (im Ordner res/color)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true"
        android:color="@color/controlBackground"
    />
    <item
        android:state_pressed="true"
        android:color="@color/controlBackground"
        />
    <item
        android:color="@color/blue"
        />
</selector>

Drawables

control_switch_background_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="3dp"
        android:color="@color/blue" />
</shape>

control_switch_background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/blue"></solid>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/lighterBlue"></solid>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@android:color/transparent"></solid>
        </shape>
    </item>
</selector>

control_switch_background_selector_middle.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/blue"></solid>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/lighterBlue"></solid>
        </shape>
    </item>
    <item>
        <layer-list>
            <item android:top="-1dp" android:bottom="-1dp" android:left="-1dp">
                <shape>
                    <solid android:color="@android:color/transparent"></solid>
                    <stroke android:width="1dp" android:color="@color/blue"></stroke>
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

Layout

<RadioGroup
        android:checkedButton="@+id/calm"
        android:id="@+id/toggle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="24dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/control_switch_background_border"
        android:orientation="horizontal">
        <RadioButton
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:id="@+id/calm"
            android:background="@drawable/control_switch_background_selector_middle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="Calm"
            android:fontFamily="sans-serif-medium"
            android:textColor="@color/control_switch_color_selector"/>
        <RadioButton
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:id="@+id/rumor"
            android:background="@drawable/control_switch_background_selector_middle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="Rumor"
            android:fontFamily="sans-serif-medium"
            android:textColor="@color/control_switch_color_selector"/>
        <RadioButton
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_marginRight="3dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:id="@+id/outbreak"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/control_switch_background_selector"
            android:button="@null"
            android:gravity="center"
            android:text="Outbreak"
            android:fontFamily="sans-serif-medium"
            android:textColor="@color/control_switch_color_selector" />
</RadioGroup>

  • Dadurch wird die Schaltfläche zum Anfang der Ansicht. Was ist, wenn die Schaltfläche am Ende des Schalters platziert werden muss (nach dem Text des Schalters)

    – Tarun Kumar

    21. Juni 19 um 10:06 Uhr

  • @TarunKumar hast du etwas zum Ausrichten des Layouts in der Mitte?

    – Parth Patel

    13. Mai 20 um 7:50 Uhr

1643907793 237 Wie benutzerdefinierte Schaltertaste
DM

Sie können Android-Materialkomponenten verwenden.

Geben Sie hier die Bildbeschreibung ein

build.gradle:

implementation 'com.google.android.material:material:1.0.0'

layout.xml:

<com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:checkedButton="@id/btn_one_way"
        app:singleSelection="true">

    <Button
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:id="@+id/btn_one_way"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="One way trip" />

    <Button
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:id="@+id/btn_round"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Round trip" />

</com.google.android.material.button.MaterialButtonToggleGroup>

.

757840cookie-checkWie benutzerdefinierte Schaltertaste?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy