在Android的Spinner下拉项目中添加图标

问题描述

我的android应用程序中有一个带有自定义主题的微调框。我需要为每个项目添加自定义图标。下拉布局为:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textAlignment="viewStart"
    android:paddingStart="10dp"
    android:paddingEnd="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:textSize="18sp"
    android:textColor="@android:color/black"
    android:singleLine="true" />

然后通过以下代码将其添加到微调适配器中:

zoneAdapter.setDropDownViewResource(R.layout....)

它运行良好。现在,我需要添加图标。任何帮助将不胜感激。

解决方法

尝试以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:background="@drawable/border"
        android:layout_height="40dp">

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageView
            android:src="@drawable/spinner_draw"
            android:layout_width="30dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_height="30dp"/>
    </RelativeLayout>
</LinearLayout>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...