android - 如何在 android 中使用开关轨道两侧的文本创建自定义开关?

标签 android native android-switch

如何在 android 中设计自定义开关,如下图所示:

State 1

当它打开时,它需要看起来像这样

State 2

我还需要在两个类别之间切换时显示切换动画效果。我怎样才能实现它?是否有任何 3rd 方 SDK 或库可用?目前我已经使用自定义线性布局设计它,如下所示:

my_layout.xml:

 <LinearLayout
                android:id="@+id/customSliderLayout"
                android:layout_width="@dimen/_200sdp"
                android:layout_height="@dimen/_39sdp"
                android:layout_centerInParent="true"
                android:orientation="horizontal"
                android:weightSum="2"
                android:background="@drawable/oval_layout_bg"
                android:layout_centerHorizontal="true">

                <Button
                    android:id="@+id/userBtn"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:text="@string/toggle_user"
                    android:textSize="@dimen/_18sdp"
                    android:textColor="@color/black_textcolor"
                    android:background="@drawable/back"/>

          <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:textColor="@color/textcolor_white"
                    android:textSize="@dimen/_16sdp"
                    android:gravity="center"
                    android:text="@string/toggle_doctor"/>

            </LinearLayout>

但我需要设计一个拨动开关。

我还检查了这个库:

https://github.com/gmarm/BetterSegmentedControl

但这仅适用于 iOS,不适用于 Android。我需要与链接中的第二个开关完全一样。

最佳答案

它有点棘手,因为你不能在轨道上写任何东西,所以你需要一个 TextView 来为你的文本在轨道上,并在需要时简单地改变它的位置。
准备好您的形状或图像,然后在约束布局中使用开关和 TextView ...在您需要它们的同一个位置匹配它们,然后查找代码,当开关打开/关闭时,将 TextView 移动到另一侧...大声笑它完美地工作...
对于拇指文本:

    cSwitch.setTextOn("doctor");
    cSwitch.setTextOff("user");

我知道它有很大的改进空间,但我就是这样做的,你可以将形状的宽度和高度更改为......

这是我的代码我没有做你需要的东西哈哈你必须自己做哈哈

我希望这有帮助..干杯

enter image description here

enter image description here

切换轨道形状
 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item
         android:width="11mm"
         android:height="4.2mm">
         <shape android:shape="rectangle">
             <corners android:radius="3.7mm" />
             <stroke
                 android:width="0.3mm"
                 android:color="@color/white" />
             <solid android:color="@color/green" />
         </shape>
     </item>
 </layer-list>

切换拇指形状
 <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
     <size
        android:width="4.2mm"
        android:height="4.2mm" />
    <corners android:radius="2mm" />
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="#bdf7b8" />
</shape>

布局 XML 代码
 <android.support.constraint.ConstraintLayout
    android:id="@+id/signInLayout"
    style="@style/LayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_marginTop="16dp">

 <Switch
        android:id="@+id/cSwitch"
        android:layout_width="wrap_content"
        android:layout_height="4mm"
        android:switchMinWidth="11mm"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/cSwitch_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="2mm"
        android:layout_marginStart="2mm"
        android:text="ON"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="@id/cSwitch"
        app:layout_constraintLeft_toLeftOf="@+id/cSwitch"
        app:layout_constraintTop_toTopOf="@id/cSwitch" />
</android.support.constraint.ConstraintLayout>

和java代码
    final Switch cSwitch = rootView.findViewById(R.id.cSwitch);
    final TextView cSwitchText = rootView.findViewById(R.id.cSwitch_textView);
    cSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ConstraintLayout constraintLayout = rootView.findViewById(R.id.signInLayout);
            ConstraintSet constraintSet = new ConstraintSet();
            constraintSet.clone(constraintLayout);
            constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.TOP, R.id.cSwitch, ConstraintSet.TOP, 0);
            constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.BOTTOM, R.id.cSwitch, ConstraintSet.BOTTOM, 0);
            cSwitch.setThumbDrawable(rootView.getResources().getDrawable(R.drawable.switch_thumb_green));

            if (isChecked) {
                cSwitchText.setText("ON");
                constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.LEFT, R.id.cSwitch, ConstraintSet.LEFT, 0);
                constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.RIGHT, ConstraintSet.UNSET, ConstraintSet.RIGHT, 0);
                cSwitch.setTrackDrawable(ContextCompat.getDrawable(rootView.getContext(), R.drawable.switch_track_green));
            } else {
                cSwitchText.setText("OFF");
                constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.RIGHT, R.id.cSwitch, ConstraintSet.RIGHT, 0);
                constraintSet.connect(R.id.cSwitch_textView, ConstraintSet.LEFT, ConstraintSet.UNSET, ConstraintSet.LEFT, 0);
                cSwitch.setTrackDrawable(ContextCompat.getDrawable(rootView.getContext(), R.drawable.switch_track_red));
                cSwitch.setThumbDrawable(rootView.getResources().getDrawable(R.drawable.switch_thumb_red));
            }
            constraintSet.applyTo(constraintLayout);

        }
    });

关于android - 如何在 android 中使用开关轨道两侧的文本创建自定义开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028892/

相关文章:

scala - 将 scala 转换为 native 二进制文件

android - 带 Android 开关的 RxView

android - Google map View 无法在模拟器上显示

java - 是否可以使用 Java 11 而不是 Java 8 运行 ionic cordova 构建?

android - anchor 导航不适用于三星 Android 设备

ios - 如何查找/访问通过 iOS 上的应用程序生成的 PDF 文件?

android - 如何将 bottomsheet 扩展到工具栏底部,而不是在工具栏下方?

java - 是否可以让 GC 管理 native 对象的生命周期?

java - Android - 以编程方式更改开关状态而不触发 OnCheckChanged 监听器

android - 显示 ListView 后,尝试以编程方式设置 LISTVIEW 内 SWITCH 的状态