Android自定义开关

时间:2018-09-25 11:14:11

标签: android android-switch

我正在尝试像这样进行自定义切换:

switch picture

具有这些特性:

    总是显示
  1. 两面的文字。
  2. 打开和关闭的不同颜色。

这是我面临的两个问题,因为开关仅在所选边显示文本,而我似乎找不到可以指定两种不同颜色的地方?

我可以使用android studio中的常规开关实现此功能,还是必须使用某些库?

谢谢。

4 个答案:

答案 0 :(得分:1)

研究之后,我找到了一种可以准确满足我需要的方法,这就是我所得到的:

custom switch

如果有人正在寻找一种方法来做到这一点,那就是这样:

基于这篇帖子answer,对我来说非常有用。

这就是我所做的,我创建了两个可绘制对象,一个用于On,另一个用于Off:

switch_on.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:state_checked="true" android:drawable="@color/colorGray"/>
    <item android:drawable="@color/colorPrimary" android:state_checked="true" />
    <item android:drawable="@color/colorPrimaryDark" android:state_pressed="true" />
    <item android:drawable="@color/transparent" />

</selector>

switch_off.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:state_checked="true" android:drawable="@color/colorGray"/>
    <item android:drawable="@color/gray_light" android:state_checked="true" />
    <item android:drawable="@color/black_overlay" android:state_pressed="true" />
    <item android:drawable="@color/transparent" />
</selector>

下一步,为开关的轮廓创建一个可绘制的对象。 outline.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="2dp" />
    <solid android:color="#80ffffff" />
    <stroke
        android:width="1dp"
        android:color="@color/gray_light" />
</shape>

我添加的一件事是文本颜色的可绘制对象,因为文本的颜色根据是否选中而变化,就是这样: switch_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector        xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/colorWhite"/>
    <item android:state_checked="true" android:color="@color/colorWhite"/>
    <item android:color="@color/gray_light"/>
</selector>

最后,以这种方式在我的布局中创建了RadioGroup

<RadioGroup
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="@drawable/outline"
        android:checkedButton="@+id/off"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/off"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginStart="1dp"
            android:layout_marginTop="1dp"
            android:layout_weight="1"
            android:background="@drawable/switch_off"
            android:button="@null"
            android:gravity="center"
            android:padding="@dimen/fab_margin"
            android:text="@string/off"
            android:textColor="@drawable/switch_text" />

        <RadioButton
            android:id="@+id/on"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginEnd="1dp"
            android:layout_marginTop="1dp"
            android:layout_weight="1"
            android:background="@drawable/switch_on"
            android:button="@null"
            android:gravity="center"
            android:padding="@dimen/fab_margin"
            android:text="@string/on"
            android:textColor="@drawable/switch_text" />
    </RadioGroup>

在正确的位置注意每个可绘制对象的用法:

android:background="@drawable/outline" for RadioGroup android:background="@drawable/switch_off"用于第一个RadioButton android:background="@drawable/switch_on"用于第二个RadioButton 两个单选按钮的android:textColor="@drawable/switch_text"

仅此而已。

答案 1 :(得分:0)

我认为this library可以帮助您

implementation 'com.github.angads25:toggle:1.0.0'

用法

  1. 首先在您的xml布局中添加一个Switch(例如LabeledSwitch):

    <com.github.angads25.toggle.LabeledSwitch
        android:id="@+id/switch4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:textSize="14sp"
        app:on="false"
        app:textOn="ON"
        app:textOff="OFF"
        app:colorOn="#00c4a6"
        app:colorBorder="#00c4a6"/>
    
  2. 在您的Activity / Fragment类中为Switch的引用设置一个Toggle Event Handler,如下所示:

    LabeledSwitch labeledSwitch = findViewById(R.id.switch);
    labeledSwitch.setOnToggledListener(new OnToggledListener() {
        @Override
        public void onSwitched(LabeledSwitch labeledSwitch, boolean isOn) {
        // Implement your switching logic here
        }
    });
    

就是这样。您所有的切换回调都将在onSwitched方法中处理,参数isOn将提供开关的当前状态。

可用的开关

enter image description here

答案 2 :(得分:0)

要为开关创建ON和OFF标签,可以在开关声明中使用属性android:textOnandroid:textOff。 为确保始终显示文本Lebals,尤其对于API Level较大的API21,还请使用以下属性:android:showText="true"。 然后您的开关应如下所示:

<Switch
    android:id="@+id/switcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="OFF"
    android:textOn="ON"
    android:showText="true"
/>


为了更改默认颜色,您必须为其指定单独的设计,如下所示:

1.在文件values\styles.xml中定义如下样式:

<style name="CustomSwitchTheme" parent="Theme.AppCompat.Light">
    <item name="android:colorControlActivated">#FF0000</item>
</style>

重要的是,您还要引用正确的父主题。


2。定义新的开关样式之后,您可以将自定义样式链接到具有属性

的开关
android:theme="@style/CustomSwitchTheme"

最后,您的Switch应该看起来像这样:

<Switch
    android:id="@+id/switcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="OFF"
    android:textOn="ON"
    android:showText="true"
    android:theme="@style/CustomSwitchTheme"
/>

答案 3 :(得分:0)

使用ToggleButton(根据图像比例更改高度/宽度)和选择器,这是代码

<ToggleButton
      android:id="@+id/toggle_"
      android:layout_width="60dp"
      android:layout_height="30dp"
      android:layout_alignParentStart="true"
      android:background="@drawable/on_off"
      android:textOff=""
      android:textOn=""/>

<selector
   xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true"
   android:drawable="@drawable/ic_on"  />
   <item android:state_checked="false"
   android:drawable="@drawable/ic_off"  />
</selector>
相关问题