偏好开关对 SwitchMaterial 的连锁反应?
Ripple effect of preference switch on SwitchMaterial?
PreferenceScreen
中的开关具有很好的波纹效果,可以覆盖整个宽度。我怎样才能在 PreferenceScreen
之外的 SwitchMaterial
开关上获得这种连锁反应(在正常布局中)?
接触 ViewGroup 是直接接触 ViewGroup 还是来自 ViewGroup 的 child 流 到 child 并返回。我发现 this post 及其相关的 Stack Overflow 答案有助于理解触摸事件。
要获得 PreferenceScreen 波纹效果,请将 parent 开关布局设置为可点击且可聚焦,并带有波纹背景。
无论是直接触摸开关还是直接触摸 ViewGroup,我们都需要相同的响应。开关有自己的响应式背景,因此开关的背景设置为 @null
,因此不会在触摸事件中突出显示。如果需要突出显示,可以将其删除。
<LinearLayout
android:id="@+id/switchLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:padding="16dp">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false" />
</LinearLayout>
由于 parent 正在捕获点击事件,因此开关不会收到点击事件,因此我们必须在点击 ViewGroup 时以编程方式点击开关:
val switchLayout = findViewById<ViewGroup>(R.id.switchLayout)
switchLayout.setOnClickListener {
findViewById<View>(R.id.switchWidget).performClick()
}
这一切将为我们提供以下内容:
PreferenceScreen
中的开关具有很好的波纹效果,可以覆盖整个宽度。我怎样才能在 PreferenceScreen
之外的 SwitchMaterial
开关上获得这种连锁反应(在正常布局中)?
接触 ViewGroup 是直接接触 ViewGroup 还是来自 ViewGroup 的 child 流 到 child 并返回。我发现 this post 及其相关的 Stack Overflow 答案有助于理解触摸事件。
要获得 PreferenceScreen 波纹效果,请将 parent 开关布局设置为可点击且可聚焦,并带有波纹背景。
无论是直接触摸开关还是直接触摸 ViewGroup,我们都需要相同的响应。开关有自己的响应式背景,因此开关的背景设置为 @null
,因此不会在触摸事件中突出显示。如果需要突出显示,可以将其删除。
<LinearLayout
android:id="@+id/switchLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:padding="16dp">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false" />
</LinearLayout>
由于 parent 正在捕获点击事件,因此开关不会收到点击事件,因此我们必须在点击 ViewGroup 时以编程方式点击开关:
val switchLayout = findViewById<ViewGroup>(R.id.switchLayout)
switchLayout.setOnClickListener {
findViewById<View>(R.id.switchWidget).performClick()
}
这一切将为我们提供以下内容: