gpt4 book ai didi

android - 偏好开关对 SwitchMaterial 的链式 react ?

转载 作者:行者123 更新时间:2023-12-04 23:57:18 24 4
gpt4 key购买 nike

PreferenceScreen 中的开关具有很好的波纹效果,可以覆盖整个宽度。我怎样才能在 PreferenceScreen(在正常布局中)之外的 SwitchMaterial 开关上获得这种链式 react ?

最佳答案

接触 ViewGroup,无论是直接接触 ViewGroup 还是从 ViewGroup 流向子流并返回子流。我找到 this post及其相关的 Stack Overflow 答案有助于理解触摸事件。

要获得 PreferenceScreen 涟漪效果,请将布局与开关的父布局设置为可点击且可聚焦,并带有涟漪背景。

无论是直接触摸开关还是 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>

由于父项正在捕获开关,因此开关不会收到点击事件,因此我们必须在单击 ViewGroup 时以编程方式单击开关:

val switchLayout = findViewById<ViewGroup>(R.id.switchLayout)
switchLayout.setOnClickListener {
findViewById<View>(R.id.switchWidget).performClick()
}

这一切将为我们提供以下内容:

enter image description here

关于android - 偏好开关对 SwitchMaterial 的链式 react ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70627176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com