Android 更改微调器下拉列表项文字颜色和文字大小
我使用的是android数据绑定,作为其中的一部分,我有一个微调器,其中的项(在strings.xml中声明的数组)为 现在我想更改下拉项的文本颜色和大小。 我希望为item创建一个单独的布局并通过java使用它(将item布局传递给array adapter并将该适配器设置为微调器) 我尝试了很多方法,到处寻找,但没有找到合适的解决方案Android 更改微调器下拉列表项文字颜色和文字大小,android,android-spinner,android-databinding,Android,Android Spinner,Android Databinding,我使用的是android数据绑定,作为其中的一部分,我有一个微调器,其中的项(在strings.xml中声明的数组)为 现在我想更改下拉项的文本颜色和大小。 我希望为item创建一个单独的布局并通过java使用它(将item布局传递给array adapter并将该适配器设置为微调器) 我尝试了很多方法,到处寻找,但没有找到合适的解决方案 非常感谢您的帮助。您可以创建一个样式,并通过xml将其设置为spinner <style name="customStyle" parent="@an
非常感谢您的帮助。您可以创建一个样式,并通过xml将其设置为spinner
<style name="customStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">#000</item>
<item name="android:textSize">12dp</item>
</style>
#000
12dp
将
customStyle
设置为微调器只需使用textview及其属性创建一个新的xml即可:
my_spinner.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#f51111"
/>
您应该使用
android.support.v7.widget.AppCompatSpinner
<android.support.v7.widget.AppCompatSpinner
style="@style/Widget.AppCompat.Spinner.Underlined"
android:theme="@style/Spinner"
android:entries="@array/special_fx_arrays"
android:textSize="@dimen/text_size_normal"/>
令人惊讶的答案,尤其是因为您不需要以编程方式实现任何东西。不过需要注意的是,这只在使用
android:theme
时有效,因为使用style
并不适用于所有属性,反之亦然。始终可以同时使用这两个属性(主题
和样式
),以确保正确覆盖所有属性。希望有帮助!
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_spinner,data);
spinner.setAdapter(adapter);
<android.support.v7.widget.AppCompatSpinner
style="@style/Widget.AppCompat.Spinner.Underlined"
android:theme="@style/Spinner"
android:entries="@array/special_fx_arrays"
android:textSize="@dimen/text_size_normal"/>
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:backgroundTint">@color/red</item>
<item name="android:textSize">14sp</item>
</style>