gpt4 book ai didi

android-studio - 使用 kotlin 在 android 中创建自定义微调器适配器

转载 作者:行者123 更新时间:2023-12-02 13:13:00 24 4
gpt4 key购买 nike

我正在尝试使用自定义适配器制作一个微调器来显示带有如下文本的图像

the result I want

但我有很多 Unresolved reference ,所以我认为我做错了一些事情

我有两个片段,微调器在第二个片段内custome_spinner.xml 为自定义布局文件

安卓工作室 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.custome_spinner)
spinner_image.setImageResource(flag)
spinner_country.setText(country)
return view
}
}
custome_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:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_gravity="center"
android:text="Custom Text"
android:textColor="#000" />

</LinearLayout>
 the widget inside fragment_second.xml 

<Spinner
android:id="@+id/spinner"
android:layout_width="368dp"
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"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/from_textView" />

最佳答案

你的问题出在customeSpinnerAdapter.kt

  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
//Add this
val inflater : LayoutInflater

fun customeSpinnerAdapter(context:Context,name:String,image:String){
this.country = name
this.flag = image

//val inflater = LayoutInflater.from(context)
inflater = LayoutInflater.from(context)
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val view = inflater.inflate(R.layout.custome_spinner)
spinner_image.setImageResource(flag)
spinner_country.setText(country)
return view
}
}

关于android-studio - 使用 kotlin 在 android 中创建自定义微调器适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com