如何将 android 中的开关设置为禁用,并且颜色为灰色,以便用户可以理解此开关已禁用
How to set a switch in android disabled and also have a grayish color so that user can understand this switch is disabled
我试过了
isEnabled = false
isFocsable = false
isClickabe = false
isActivited = false
但没有任何东西呈现那种灰色。
您只需要使用 Material 从 Material 设计库切换。添加 material 设计依赖项并使用 Material 设计库中的开关。该库会自动处理您所描述的场景。有关 Material 设计库的开关的更多信息,请参见此处:
https://material.io/components/switches
您只需要包含 Material 的依赖项 在您的 xml 文件中设计和使用以下内容
<com.google.android.material.switchmaterial.SwitchMaterial
...
/>
你可以试试这个风格。比如在themes.xml
中定义一个样式
<style name="Switchtheme" parent="Theme.AppCompat.Light">
<item name="colorControlActivated">Your color code</item>
<!-- inactive thumb color -->
<item name="colorSwitchThumbNormal">Your color code
</item>
<item name="android:colorForeground">Your color code
</item>
在XML
<Switch
android:layout_width="match_parent"
android:checked="false"
android:theme="@style/Switchtheme"
android:layout_height="wrap_content"/>
我试过了
isEnabled = false
isFocsable = false
isClickabe = false
isActivited = false
但没有任何东西呈现那种灰色。
您只需要使用 Material 从 Material 设计库切换。添加 material 设计依赖项并使用 Material 设计库中的开关。该库会自动处理您所描述的场景。有关 Material 设计库的开关的更多信息,请参见此处: https://material.io/components/switches
您只需要包含 Material 的依赖项 在您的 xml 文件中设计和使用以下内容
<com.google.android.material.switchmaterial.SwitchMaterial
...
/>
你可以试试这个风格。比如在themes.xml
中定义一个样式<style name="Switchtheme" parent="Theme.AppCompat.Light">
<item name="colorControlActivated">Your color code</item>
<!-- inactive thumb color -->
<item name="colorSwitchThumbNormal">Your color code
</item>
<item name="android:colorForeground">Your color code
</item>
在XML
<Switch
android:layout_width="match_parent"
android:checked="false"
android:theme="@style/Switchtheme"
android:layout_height="wrap_content"/>