我正在尝试使用自定义适配器创建一个Spinner
,以显示带有如下文本的图像
但是我有很多未解决的参考资料,所以我想我做错了什么
我有两个片段微调器在第二个片段custome_spinner.xml
是自定义布局文件
Android Studio版本3.1.2,kotlin_version = '1.2.30'
和gradle:3.1.2
CustomeSpinnerAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class customeSpinnerAdapter : BaseAdapter {
override fun getItem(p0: Int): Any {
return flag.length
}
override fun getItemId(p0: Int): Long {
return null
}
override fun getCount(): Int {
return 0
}
val country: String
val flag: String
fun customeSpinnerAdapter(context: Context, name: String, image: String) {
this.country = name
this.flag = image
val inflater = LayoutInflater.from(context)
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val view = inflater.inflate(R.layout.custom_spinner)
spinner_image.setImageResource(flag)
spinner_country.setText(country)
return view
}
}
custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/spinner_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@drawable/us" />
<TextView
android:id="@+id/spinner_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Custom Text"
android:textColor="#000" />
</LinearLayout>
- *fragment_second.xml**中的小部件
<Spinner
android:id="@+id/spinner"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_width="368dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/from_textView" />
1条答案
按热度按时间r55awzrz1#
您的问题在于customeSpinnerAdapter.kt