Autoscale spinner всплывающий текст? - PullRequest
0 голосов
/ 17 апреля 2020

Существует возможность автоматического масштабирования размера текста с помощью AppCompatTextView , чтобы соответствовать размеру представления с указанной границей. Все работает так, как и ожидалось для TextView для выбранного элемента для счетчика, но при открытии всплывающего окна выбрать новый элемент. Элементы из всплывающего окна получают размер текста, который задается атрибутом textSize и не использует автоматический размер. Есть ли способ, которым я могу использовать автоматический размер текста для элементов во всплывающем окне?

Вот изображение счетчика с выбранным элементом (работает как положено):
enter image description here

Вот изображение с представлением всплывающего окна (Здесь элементы получают размер текста, указанный в атрибуте: textSize , а не автоматический размер)
enter image description here

Вот код для счетчика в activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatSpinner
        android:id="@+id/spinner"
        android:layout_width="200dp"
        android:layout_height="70dp"
        android:background="#ff0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Вот макет для элементов из счетчика в spinner_item. xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text_view_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="start|center_vertical"
        android:textColor="#5A5A5A"
        android:textSize="15sp"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Код в SimpleAdapter.kt

    import android.content.Context
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.AdapterView
    import android.widget.ArrayAdapter 
    import android.widget.TextView

    class SimpleAdapter(context: Context, var nameList: ArrayList<String>) : ArrayAdapter<String>(context, 0, nameList), AdapterView.OnItemSelectedListener {

        override fun getCount(): Int {
            return nameList.size
        }

        override fun getItem(position: Int): String {
            return nameList[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            return initView(position, convertView, parent)
        }

        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
            return initView(position, convertView, parent)
        }

        private fun initView(position: Int, convertView: View?, parent: ViewGroup): View {

            // inflate the layout if view does not exist
            var convertView = convertView
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                    R.layout.spinner_item, parent, false
                )
            }

            val textView: TextView? = convertView?.findViewById(R.id.text_view_name)
            val currentItem: String? = getItem(position)
            if (currentItem != null) {
                textView?.text = currentItem
            }

            return convertView!!
        }

        override fun onNothingSelected(parent: AdapterView<*>?) {
            TODO("Not yet implemented")
        }

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            val textView: TextView? = view?.findViewById(R.id.text_view_name)
            textView?.setText( nameList[position])
        }
    }
Code in MainActivity.kt

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle 
    import android.widget.Spinner 

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            // init spinner adapter with string values
            val spinnerAdapter = SimpleAdapter(this, arrayListOf("Neo", "Smith", "Trinity", "Morpheus", "Cypher"))

            val spinner: Spinner = findViewById(R.id.spinner)
            spinner.adapter = spinnerAdapter
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...