Android - 自定义 Spinner 小部件的外观和感觉 [英] Android - Customizing the Spinner widget Look and Feel

查看:36
本文介绍了Android - 自定义 Spinner 小部件的外观和感觉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改 Android 微调小部件单选按钮的颜色.默认情况下,它为单选按钮显示绿色.

Is it possible to change the color of the radio button in the Android spinner widget. By default it displays the green color for the radio button.

我需要将其更改为其他颜色,是否可以,以及如何更改?

I need to change it to some other color, is it possible, and how?

推荐答案

我现在知道这是一个老问题,但这里是...

I know this is an old question now, but here goes...

您需要创建一个自定义主题并将其应用到带有微调器的活动中.

You will need to create a custom Theme and apply it to the Activity with your spinner.

首先,您需要为新"收音机的选中/未选中状态创建图像,您只需提取给定的图像btn_radio_on.pngbtn_radio_off.png 来自 sdk 的 res/drawable-* 文件夹.编辑它们以查看您想要的方式(例如更改颜色或其他)并保存到您的项目中.

First, you need to create images for the checked/unchecked states of the 'new' radio, you could just pull the given images btn_radio_on.png and btn_radio_off.png from the sdk's res/drawable-* folder(s). Edit them to look how you want (such as changing color or whatever) and save off to your project.

接下来,在您的 res/values 文件夹中创建一个新的 xml 文件,并添加以下内容:

Next, create a new xml file in your res/values folder, and add the following:

<resources>
    <style name="CustomSpinnerRadioTheme" parent="@android:style/Theme">
        <item name="android:spinnerDropDownItemStyle">@style/EditedRadio</item>
    </style>

    <style name="EditedRadio" parent="@android:style/Widget.DropDownItem.Spinner">
        <item name="android:checkMark">@drawable/edited_radio</item>
    </style>
</resources>

然后,在res/drawable中创建另一个名为edited_radio.xml的xml文件,它应该包含以下内容:

Then, create another xml file in res/drawable named edited_radio.xml, and it should contain the following:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

请务必参考您已编辑的已选中状态的图像.然后你只需要将 CustomSpinnerRadioTheme 应用到你的 Activity 并运行!

just be sure to reference your edited images for the checked states. Then you just have to apply the CustomSpinnerRadioTheme to your Activity and run!

我发现的一个很好的资源是应用样式和主题,尤其是关于 Android 样式(样式.xml) 和 Android 主题 (themes.xml)

A good resource I found is Applying Styles and Themes especially the additional reference on Android Styles (styles.xml) and Android Themes (themes.xml)

这篇关于Android - 自定义 Spinner 小部件的外观和感觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆