如何自定义微调器?

问题描述 投票:0回答:1

我正在创建一个具有自定义背景的自定义微调器,因为这将隐藏困难的下拉列表,这就是为什么我设置自定义布局来显示下拉列表。但是当我将适配器添加到微调器时,应用程序崩溃了?为什么? 这是微调器代码

<Spinner
        android:id="@+id/spinner"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_marginTop="70dp"
        android:padding="10dp"
        android:background="@drawable/border"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

这是可绘制边框

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:color="@color/text" android:width="1dp"/>
    <corners android:radius="15dp"/>

</shape>

这是自定义列表

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/imageViewDropdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/textViewItem"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/imageViewDropdown"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的片段代码

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val spinner=binding.spinner
        val spinnerAdapter=ArrayAdapter<String>(requireContext(),android.R.layout.simple_dropdown_item_1line,this.listitem)
        spinner.adapter=spinnerAdapter
    }

我遇到的错误

Process: com.example.assignmentbackcoffer, PID: 5663
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

我不想改变边框效果。 这对我来说非常重要,如果有人帮助我,我会很高兴,提前谢谢。

android xml kotlin spinner android-spinner
1个回答
0
投票

请注意

ArrayAdapter
数据类型。它只有
String
,但是当它只需要字符串时,你给他包含文本和图像的布局。默认
ArrayADapter
不知道如何处理。来自这个SO答案的代码应该可以工作:

Spinner              spinnerCountShoes = (Spinner)findViewById(R.id.spinner_countshoes);
ArrayAdapter<String> spinnerCountShoesArrayAdapter = new ArrayAdapter<String>(
                     this,
                     android.R.layout.simple_spinner_dropdown_item, 
                     getResources().getStringArray(R.array.shoes));
spinnerCountShoes.setAdapter(spinnerCountShoesArrayAdapter);

查看此答案,了解有关如何实现您询问的用例的详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.