How to show divider between spinner items?

亡梦爱人 提交于 2019-11-27 21:58:17
X09

This worked for me:

<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
        <item name="android:divider">#d1d1d1</item>
        <item name="android:dividerHeight">0.5dp</item>
    </style>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>

The advantage of using this is that it doesn't remove the ripple effect on hover.

I managed to find a more proper solution for this issue (without including the divider in the single item layout).

What you have to do is define in your activity's theme

        <item name="android:dropDownListViewStyle">@style/App.Style.Spinner</item>

and then create the proper style with

   <style name="App.Style.Spinner" parent="@style/Widget.Sherlock.Light.ListView.DropDown">
           <item name="android:dividerHeight">10dip</item>
           <item name="android:divider">@drawable/mydivider</item>
   </style>

Based on @Talihawk answer, I made it work for specific spinner only. Instead of setting your activity theme, set the theme directly for the spinner view:

<style name="MatchSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
    <item name="android:divider">#123456</item>
    <item name="android:dividerHeight">1dp</item>
</style>

<style name="MatchSpinnerTheme" parent="AppTheme">
    <item name="android:dropDownListViewStyle">@style/MatchSpinnerStyle</item>
</style>

and

<android.support.v7.widget.AppCompatSpinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"        
    android:theme="@style/MatchSpinnerTheme"/>

For people with same problem i after almost gived up i got an idea of how to get the divider.

I added the divider line at the bottom of my custom layout for each item

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem2">

    <TextView android:id="@+id/Text" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        style="@style/SpinnerView_Text" android:paddingLeft="10dip" />

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/arrowright"
        android:layout_alignParentRight="true" android:layout_centerInParent="true"
        android:layout_marginRight="20dip" />

</RelativeLayout>

<ImageView android:id="@+id/Divider1" android:layout_width="fill_parent"
    android:layout_height="1dip" style="@style/Divider"></ImageView>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!