自定义微调器android

时间:2018-01-29 14:22:19

标签: android kotlin spinner android-spinner

  

问题:设置适配器时,它隐藏了自定义背景

我正在尝试向微调器添加自定义背景,

 <Spinner
            android:id="@+id/spinnerReferredBy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spinnerMode="dropdown"
            android:background="@drawable/custom_drop_down"/>
  

我的自定义drawable是,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#ffffff"/>
            <stroke
                android:width="2dp"
                android:color="#80000000"/>
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp"/>

        </shape>
    </item>

    <item>
        <bitmap
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="right|end"
            android:src="@drawable/ic_drop_down" />
    </item>
</layer-list>
  

像这样设置适配器

 spinnerReferredBy.adapter = ArrayAdapter<String>(activity,R.layout.custom_spinner_item,referred)
  

输出

  1. 没有设置适配器 enter image description here

  2. 使用适配器 enter image description here

1 个答案:

答案 0 :(得分:0)

Simply add this line in the activity layout in the Spinner section

android:entries="@array/yourSpinnerArray"

<Spinner
                android:id="@+id/spinnerForThisMonth"
                style="@style/listStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"         
                android:entries="@array/yourSpinnerArray"
                android:gravity="center" />

Then create String resources in values

   <string-array name="yourSpinnerArray">
        <item>Select</item>
        <item>January</item>
        <item>February</item>
        <item>March</item>
        <item>April</item>
        <item>June</item>
        <item>July</item>
        <item>August</item>
        <item>September</item>
        <item>October</item>
        <item>November</item>
        <item>December</item>