Android 如何将SwitchPreference样式设置为";“正常”;转换 更新问题
对于Android 如何将SwitchPreference样式设置为";“正常”;转换 更新问题,android,Android,对于AppCompatActivity,我有以下布局: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_p
AppCompatActivity
,我有以下布局
:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<Switch
android:id="@+id/swcExample"
android:layout_width="match_parent"
android:layout_height="48dp"
android:padding="8dp"
android:text="example" />
</LinearLayout>
</ScrollView>
…这只是一个普通的开关组件,当我激活它时,它看起来如下所示:
但是我希望开关的行为如下所示:
(我用框架布局
欺骗了它)
问题是,如何使用正常的开关获得第二个涟漪效应(参见上面的代码)
提前感谢。这种连锁反应通常在Android 5.0+上出现
如果希望涟漪仅显示在行上,并且minSdkVersion
为21或更高,请将以下属性添加到LinearLayout
:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
但是,当单击行时,这不会切换开关。您需要在线性布局中添加一个视图。单击Listener
,然后根据需要切换开关
如果您的minSdkVersion
低于21,并且您希望发生平台标准选择事件(ripple on 5.0+,row flash on 4.4及更高版本),我上面显示的应该可以工作。如果您希望在旧设备上使用ripple。。。可能有一个配方,但我不知道它是什么。将android:foreground=“?android:attr/selectableItemBackground”
属性添加到您的交换机。与“动画”分开的“涟漪效应”是什么?“如何定制”中的“it”是什么RippleDrawable
是实现ripple的常用解决方案,但不清楚这是否是您想要的。@Commonware我用代码和图像更新了这个问题。我在布局中使用了该解决方案,但@ayush maharjan的答案完全符合我的要求,谢谢你的分享。我一直在使用后台
而不是前台
,这正是我想要的,非常感谢。