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

标签 android material-design material-components-android

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/

相关文章:

android - 使用 Mockito.verify() 时,不只是检查是否在模拟对象上调用了该函数,而是调用来自真实对象的方法

java - Android:以编程方式添加 TextInputLayout

android - 无法添加名称为 'testCompile' 的配置,因为具有该名称的配置已存在

android - 如何在 CollapsingToolbarLayout 折叠结束时隐藏 ImageView?

android - 使用 androidx DialogFragment 创建 AlertDialog 时按钮样式错误

android - 如何制作跳跃标尺

android - 如何为AlertDialog设置自定义布局?

java - Android Mantis客户端 "Could not find class"错误

android - 进度条在android中两边都变圆了

android - 如何在 Android 上设置 google SwitchMaterial 按钮的样式?