1

我正在尝试使用 Android 的 Switch 组件,但是在单击它时,我得到了不需要的视图,它一次显示 2 个状态(选中和未选中)。

我也使用了自定义样式,但没有成功。下面是我的自定义样式。我在片段中使用了相同的视图,我没有遇到任何问题,但在另一个活动中我遇到了这个问题。谁能提出问题可能是什么?我使用了所有变体,如 Switch、SwitchMaterial、SwitchCompat,但同样的问题正在发生。

<style name="Switch_Veg_Only" parent="Theme.AppCompat.Light">
    <!-- active thumb & track color (30% transparency) -->
    <item name="colorControlActivated">@color/green</item>

    <!-- inactive thumb color -->
    <item name="colorSwitchThumbNormal">@color/grey
    </item>

    <!-- inactive track color (30% transparency) -->
    <item name="android:colorForeground">#42221f1f
    </item>
</style>

下面是 Switch xml 文件:

<com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/activity_filter_switch_veg_only"
        android:theme="@style/Switch_Veg_Only"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/activity_filter_text_view_filter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/activity_filter_text_view_filter"
        android:layout_gravity="end"
        android:checked="false"
        android:text="@string/fragment_home_veg_only"
        android:textColor="@color/DarkGrey"
        android:textAllCaps="true"
        android:textOn="a"
        android:textOff="b"
        android:textSize="@dimen/fragment_home_veg_only"
        app:switchPadding="8dp" />

输出图像

4

0 回答 0