我使用微调器对国家标志进行下拉。 但是下拉菜单的高度并非如图片所示的那样正确,但是当我开始滚动下拉菜单或将其关闭并再次打开时,它会自动纠正。
我为转盘使用了自定义适配器
class CustomAdapter(context: AppCompatActivity, resouceId: Int, val list: List<DirectoryResponse>) : ArrayAdapter<DirectoryResponse>(context, resouceId, list) {
private var inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//TODO : Recycle the view
val rowItem = getItem(position)
val rowview = inflater.inflate(R.layout.flag_spinner, parent, false)
val imageView = rowview.findViewById(R.id.flag_image) as ImageView
val padding = dpToPx(6).toInt()
imageView.setPadding(padding, padding, padding, padding)
val imageSize = dpToPx(36).toInt()
GlideApp.with(context)
.load(rowItem.FlagUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.override(imageSize)
.into(imageView)
return rowview
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
return getView(position, convertView, parent)
}
}
flag_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/flag_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:scaleType="fitCenter"
tools:src="@drawable/ic_flag">
</ImageView>
SignInActivity.kt中的部分
val adapter = CustomAdapter(this, R.layout.flag_spinner, it)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
flag_spinner.adapter = adapter
答案 0 :(得分:0)
由于您无法轻松设置下拉高度,因此我相信它无法正确显示,因为第一次打开图像时未加载该图像。
尝试将ImageView
的大小从wrap_content
更改为固定的宽度和高度。例如:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/flag_image"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:scaleType="fitCenter"
tools:src="@drawable/ic_flag"/>