我希望自定义Switch
按钮变为如下:
如何实现这一目标?
答案 0 :(得分:115)
然而,这可能不是最好的方法,但我采取了不同的方式来解决我的所有Switch相关问题。 这是我的代码 -
<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>
答案 1 :(得分:21)
您可以使用以下代码更改颜色和文字:
<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"
/>
在res / values文件夹中创建名为colors.xml的xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
</resources>
在drawable文件夹中,创建一个xml文件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>
并在定义切换按钮的xml部分添加:
android:background="@drawable/my_btn_toggle
更改textOn
和textOff
使用
android:switchTextAppearance="@style/Switch"
答案 2 :(得分:19)
<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"/>
抽拉/ 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>
抽拉/ 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>
下一张图片是9.paths drawables,它们必须是不同的密度(mdpi,hdpi,xhdpi,xxhdpi)。例如,我给xxhdpi(你可以根据需要调整大小):
抽拉/ custom_switch_thumb_disabled_holo_light
抽拉/ custom_switch_thumb_pressed_holo_light
抽拉/ custom_switch_thumb_activated_holo_light
抽拉/ custom_switch_thumb_holo_light
抽拉/ custom_switch_bg_focused_holo_light
抽拉/ custom_switch_bg_holo_light
答案 3 :(得分:7)
我实现了这个
做:
1)自定义选择器:
<?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)使用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"/>
答案 4 :(得分:7)
我使用此方法使用RadioGroup
和RadioButton
创建自定义切换;
<color name="blue">#FF005a9c</color>
<color name="lightBlue">#ff6691c4</color>
<color name="lighterBlue">#ffcdd8ec</color>
<强> 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>
<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>
答案 5 :(得分:5)
您可以使用常规的Switch小部件,只需拨打setTextOn()
和setTextOff()
,或使用android:textOn
和android:textOff
属性。
答案 6 :(得分:3)
您可以使用Android Material Components。
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>
答案 7 :(得分:2)
它是一个简单的xml设计,其外观类似于iOS开关,请在下面的图片中进行检查
您需要创建custom_thumb.xml和custom_track.xml
这是我的开关,我需要一个很大的开关,因此添加了layout_width / layout_height参数
<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"/>
现在创建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>
现在创建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>
答案 8 :(得分:1)
有两种方法可以创建自定义ToggleButton
1)通过定义自定义背景 2)通过创建自定义按钮
检查http://www.zoftino.com/android-toggle-button是否有自定义样式
切换具有自定义背景的按钮
将drawable定义为xml资源,如下所示,并将其设置为切换按钮的背景。在下面的示例中,drawable toggle_color是一个颜色选择器,您还需要定义它。
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="4dp"
android:insetRight="4dp"
android:insetBottom="4dp">
<layer-list android:paddingMode="stack">
<item>
<ripple android:color="?attr/android:colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="?attr/android:colorButtonNormal">
<corners android:radius="8dp"/>
<solid android:color="@android:color/white" />
<padding android:left="8dp"
android:top="6dp"
android:right="8dp"
android:bottom="6dp" />
</shape>
</item>
</ripple>
</item>
<item android:gravity="left|fill_vertical">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<size android:width="8dp" />
<solid android:color="@color/toggle_color" />
</shape>
</item>
<item android:gravity="right|fill_vertical">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<size android:width="8dp" />
<solid android:color="@color/toggle_color" />
</shape>
</item>
</layer-list>
</inset>
使用自定义按钮切换按钮
为两种状态的切换按钮创建自己的图像(确保所有尺寸的屏幕都存在图像)并将它们放在可绘制的文件夹中,创建选择器并将其设置为按钮。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/toggle_on" />
<item android:drawable="@drawable/toggle_off" />
</selector>
答案 9 :(得分:0)
此链接的更多信息:http://www.mokasocial.com/2011/07/sexily-styled-toggle-buttons-for-android/
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_me"/>
和drawable将是这样的:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/toggle_me_on" /> <!-- checked -->
<item android:drawable="@drawable/toggle_me_off" /> <!-- default/unchecked -->
</selector>
答案 10 :(得分:0)
<Switch
android:thumb="@drawable/thumb"
android:track="@drawable/track"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
答案 11 :(得分:0)
<androidx.appcompat.widget.SwitchCompat
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
app:track="@drawable/track"
android:thumb="@drawable/thumb"
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<?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/switch_thumb_false"/>
<item android:state_checked="true"
android:drawable="@drawable/switch_thumb_true"/>
</selector>
<?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">
<size android:width="24dp" android:height="12dp" />
<solid android:color="#EFE0BB" />
<corners android:radius="6dp" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<size android:width="24dp" android:height="12dp" />
<solid android:color="@color/colorPrimary" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#EFE0BB" />
<size
android:width="10dp"
android:height="10dp" />
<stroke
android:width="2dp"
android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="oval">
<solid android:color="@color/colorPrimary"/>
<size android:height="12dp"
android:width="12dp"/>
<stroke android:color="#EFE0BB"
android:width="2dp"/>
</shape>
</item>
</layer-list>
答案 12 :(得分:-1)
使用材料成分库,您可以使用MaterialButtonToggleGroup
:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:checkedButton="@id/b1"
app:selectionRequired="true"
app:singleSelection="true">
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="OPT1" />
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="OPT2" />
</com.google.android.material.button.MaterialButtonToggleGroup>