Текст Spinner не подходит для его родителя - PullRequest
1 голос
/ 07 октября 2019

Я использую 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>

Пример изображения:

enter image description here

Адаптер:

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>

Ответы [ 2 ]

1 голос
/ 07 октября 2019

вам нужно изменить 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>
0 голосов
/ 07 октября 2019

Удалить android:minHeight="50dp" и установить android:layout_height="wrap_content" в Spinner.

...