我正在使用多行的Spinner
,但其父边框出现问题。
我希望行为类似于TextView
或任何其他View
。如果其wrap_content
(高度),则如果从下拉列表中选择了多行选项,则应更改其高度。但这不起作用,并且文本的一部分被切成两半并隐藏在父级后面。
<LinearLayout
android:id="@+id/spinnerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:paddingEnd="8dp"
android:background="@drawable/spinner_background_ok"
android:gravity="center">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
app:errorEnabled="true"
android:overlapAnchor="false"
android:spinnerMode="dropdown"
android:singleLine="false"/>
</LinearLayout>
图片示例:
适配器:
class SpinnerAdapter(private val a: Activity, resource: Int, private val optionList: MutableList<Item>) :
ArrayAdapter<Item>(a, resource, optionList) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
return customView(position, parent)
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
return customView(position, parent)
}
private fun customView(position: Int, parent: ViewGroup): View {
val customItemView = a.layoutInflater.inflate(R.layout.spinner_item, parent, false)
val currentlyPicked = optionList[position]
val optionString = customItemView?.findViewById<TextView>(R.id.option)
optionString?.isSingleLine = false
optionString?.text = currentlyPicked.getName()
if (position == count) optionString?.setTextColor(ContextCompat.getColor(a, R.color.Grey))
else name?.setTextColor(ContextCompat.getColor(a, R.color.Black))
return customItemView
}
override fun getCount(): Int {
val count = super.getCount()
return if (count > 0) count - 1 else count
}
}
项目视图:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/normal"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:textSize="16sp"
android:text="Option"/>
</LinearLayout>
答案 0 :(得分:1)
您需要像这样更改spinner_item
:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" // do not fix the height of this layout.
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/normal"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:textSize="16sp"
android:text="Option"/>
</LinearLayout>
答案 1 :(得分:0)
删除android:minHeight="50dp"
并在 android:layout_height="wrap_content"
中设置Spinner
。