Android 垂直切换小部件

Android Vertical Switch Widget

使用 android,我想要实现垂直开关小部件。据我所知,开关看起来只有水平方向。我想要如下内容:

在浏览了这里的主题并搜索 google 之后,我还没有找到可以给我这个的东西。我搜索的所有内容都只给我水平实现。

所以我可以生成典型的水平开关

     <Switch
            android:id="@+id/switch_button"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/label"
            android:layout_gravity="center"
            android:switchMinWidth="130dp"
            android:thumb="@drawable/switch_selector" 
            android:track="@drawable/track_selector" >
     </Switch>

但似乎没有设置方向的好方法。我知道这个问题有点高。是否有一些立即可用的属性可以让我进行垂直切换?或者我是否必须创建一个自定义开关并可能修改 onDraw 方法以便它垂直翻转?任何帮助表示赞赏。谢谢

垂直方向没有快速属性..抱歉:) 首先你可以看看开关的代码,看看你能不能复制和操作它。 其次,你可以只实施你的。有一个布局里面有一个按钮。使用 onTouchListener 将其左右滑动。无需使用 "onDraw".

尝试切换按钮 图形并使用您包含在 post 中的类似图片。以下是此类切换按钮的示例:Toggle button using two image on different state

像这样尝试 android:rotation="90":

 <Switch
            android:id="@+id/switch_button"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/label"
            android:layout_gravity="center"
            android:switchMinWidth="130dp"
            android:thumb="@drawable/switch_selector" 
            android:track="@drawable/track_selector" 
            android:rotation="90">
     </Switch>