1''*_*1'' 19 android togglebutton android-xml
要创建自定义ToggleButton,我在以下位置定义了一个新样式/res/values/styles.xml
:
<style name="myToggleButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000000</item>
<item name="android:background">@drawable/my_toggle_button</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后我使用选择器指定按钮状态的外观/res/drawable/my_toggle_button.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
[...]
</shape>
</item>
<item android:state_checked="false"
<shape>
[...]
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
当状态发生变化时,如何修改此设置以切换按钮的文本颜色?
Dev*_*red 71
为您想要的文本颜色创建一个类似的状态列表,并将其放入res/color
,例如
res/color/toggle_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#070" />
<!-- Default State -->
<item android:color="#A00" />
</selector>
Run Code Online (Sandbox Code Playgroud)
然后将此资源设置为按钮的文本颜色:
<item name="android:textColor">@color/toggle_color</item>
Run Code Online (Sandbox Code Playgroud)
PS,最好让选择器中的最后一项没有附加任何状态标志(即默认状态),而不是用上述状态的倒数来定义它.
归档时间: |
|
查看次数: |
17337 次 |
最近记录: |