在微调器中更改突出显示

时间:2014-08-12 07:43:00

标签: android android-spinner

问题

我试图在我的应用程序中更改选择突出显示但没有运气。 我一直在通过风格做这件事,因为我的应用程序中有很多这样的风格。 如果您告诉我我的代码有什么问题,我将不胜感激。

CODE

<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionBar</item>
    <item name="android:imageButtonStyle">@style/MyImgBtn</item>
    <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
    <item name="android:spinnerStyle">@style/MySpinnerTheme</item>
    <item name="android:windowBackground">@android:color/white</item>
</style>

<style name="MySpinnerTheme" parent="android:Widget.Holo.Light.Spinner">
    <item name="android:activatedBackgroundIndicator">@drawable/custom_activated_background</item>
</style>

更新

所以我设法在setDropDownViewResource布局元素上合并了两个带有设置样式的选择器。但我现在得到的是同时出现的两个选择器。 我试图在XML中设置android:dropDownSelector="@android:color/transparent"的Spinner,但仍然没有运气。在下面发布更多代码。

SPINNER

    final Spinner yearSpinner = (Spinner) rootView.findViewById(R.id.yearSpinner);
    ArrayAdapter<SpinnerItem> adapterYear = new ArrayAdapter<SpinnerItem>(getActivity(),
            R.layout.spinner_item_layout , yearsItems);
    yearSpinner.setOnItemSelectedListener(itemSelectedListener);
    yearSpinner.setAdapter(adapterYear);

   <Spinner
        android:id="@+id/yearSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dropDownSelector="@android:color/transparent"
        android:layout_below="@id/yearTxt"
        android:layout_marginLeft="20dp"
        android:popupBackground="@drawable/podpowiedzi"
        android:layout_marginRight="20dp"
        android:layout_centerHorizontal="true"
        >
    </Spinner>

SPINNER_ITEM_LAYOUT

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/mySpinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textStyle="italic"
    android:textAlignment="inherit"
    android:textColor="@color/text_color"
    >
</TextView>

1 个答案:

答案 0 :(得分:1)

要更改微调器下拉项的样式和颜色,请将以下内容添加到style.xml

<style name="mySpinnerItemStyle" parent="@style/android:Theme.Holo">
    <item name="android:background">@drawable/spinner_selector</item>
    <item name="android:gravity">center_vertical</item>

</style>

然后创建&#34; spinner_selector.xml&#34;在您的drawable文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@color/orange" /> <!-- current selected -->
    <item android:state_pressed="true" android:drawable="@color/orange_bright" />
    <item android:drawable="@android:color/transparent" /> 
</selector>

如果要在按下微调器时自定义高亮颜色,我们需要9个用于微调器背景的补丁图像。执行以下步骤:

1)访问此网站:http://android-holo-colors.com/,选择Spinner,为您的微调器突出显示选择所需的颜色并下载zip文件。 (还有很多其他选项取决于您的应用)

2)在zip文件中打开res - &gt; drawable并保存文件&#34; apptheme_spinner_background_holo_light.xml&#34;到你的可绘制文件夹

3)将以下图像保存在右侧可绘制文件夹中: apptheme_spinner_default_holo_light.9.png apptheme_spinner_disabled_holo_light.9.png apptheme_spinner_focused_holo_light.9.png apptheme_spinner_pressed_holo_light.9.png

4)将其添加到style.xml文件中:

<style name="MySpinnerTheme" parent="@android:Widget.Holo.Light.Spinner">
    <item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
</style>

将来,您可以使用所需的所有资源(包括微调颜色突出显示颜色)自动生成您想要的任何样式。 http://android-holo-colors.com/