如何在Android Spinner Edittext上以编程方式设置drawableStartCompat

问题描述

我正在初始化一个带图标(国旗)和电话nr的下拉菜单。前缀(字符串)。当我以XML设置可绘制对象时,它可以工作:

app:drawableStartCompat="@drawable/flag_afn"

但是当我以编程方式找到视图时,使用

setCompoundDrawablesWithIntrinsicBounds 

它不起作用。

MainActivity.kt

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

        val viewGroup =
            (findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0) as ViewGroup

        initializeSpinner(viewGroup)
    }

    private fun initializeSpinner(parent: ViewGroup) {
        ArrayAdapter.createFromresource(
            this,R.array.phone_country_codes,R.layout.spinner_dropdown_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item)
            // Apply the adapter to the spinner
            phonePrefix.adapter = adapter

            for (cnt in 0 until adapter.count) {
                val drawable = ContextCompat.getDrawable(this,R.drawable.flag_bmd)

                val view = adapter.getView(cnt,null,parent) as AppCompatTextView
                view.text = "TEST"
//                view.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable,null)
//                view.setCompoundDrawablesWithIntrinsicBounds(drawable,null)
                view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.flag_bmd,0)
            }
        }
    }

activity_main.xml

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

    <Spinner
        android:id="@+id/phonePrefix"
        android:layout_width="180dp"
        android:layout_height="40dp"
        android:layout_margin="8dp" />

</LinearLayout>

spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView 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:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:maxLength="4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="inherit"
    tools:text="+31"
    app:drawableStartCompat="@drawable/flag_afn" />

phone_country_codes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="phone_country_codes">
        <item>+31</item>
        <item>+49</item>
        <item>+41</item>
    </string-array>
</resources>

此屏幕快照显示了“ +31”而不是“ TEST”以及在xml中添加的标志。

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...