对于layout
,我有以下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>
...这只是普通的Switch
组件,当我激活它时,它看起来像这样:
但是我希望Switch
的行为如下:
(我用FrameLayout
欺骗了它)
问题是,如何使用普通的Switch
(请参见上面的代码)来获得第二次波纹效果。
谢谢。
答案 0 :(得分:1)
这种涟漪效应通常在Android 5.0及更高版本上可见。
如果您希望波纹仅出现在行上,并且您的minSdkVersion
为21或更高,请向LinearLayout
添加以下属性:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
但是,当单击该行时,不会切换开关。您需要将View.OnClickListener
添加到LinearLayout
并适当地切换开关。
如果您的minSdkVersion
低于21,并且您希望发生平台标准的选择事件(5.0+起波纹,4.4及更早版本的行闪烁),则上面显示的内容应该有效。如果您希望老设备上的涟漪...可能有解决办法,但我不知道这是什么。
答案 1 :(得分:1)
将android:foreground="?android:attr/selectableItemBackground"
属性添加到交换机。