如何将SwitchPreference样式设置为“普通”开关?

时间:2018-12-29 15:02:30

标签: android

更新的问题

对于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组件,当我激活它时,它看起来像这样:

The standard switch ripple effect

但是我希望Switch的行为如下:

The full-component ripple effect

(我用FrameLayout欺骗了它)


问题是,如何使用普通的Switch(请参见上面的代码)来获得第二次波纹效果。

谢谢。

2 个答案:

答案 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"属性添加到交换机。