如何更改 Android 上打开的 Spinner 的文本背景颜色 [英] How to change the text background color of a opened Spinner on Android

查看:90
本文介绍了如何更改 Android 上打开的 Spinner 的文本背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中设置了这个 XML 样式,但是当我打开一个 Spinner 时我看不到文本.这是搜索信息的地方.我真的不知道我做错了什么.

I set this XML Style in my app, but when I open a Spinner I cant see the text. This is where searching of information takes place.I don't really know what I am doing wrong.

这是 XML 样式:

<style name="Theme.Color" parent="android:Theme">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:windowBackground">@drawable/fondo2</item>
</style>

这是 Spinner 的 XML:

This the XML of the Spinner:

            <Spinner
            android:id="@+id/spAnswers"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvSecurity"
            android:entries="@array/box" />

当我打开 Spinner 时,我看不到文本(文本颜色为白色).

And when I open a Spinner this happens, I cant see the text (text color is white).

只有当微调器打开或文本背景发生变化时,我才需要将文本颜色设置为另一个微调器.

I need to set the text color to another of the spinner only when it is open or the background of the text changes.

推荐答案

为 Spinner 创建适配器时,提供自定义布局而不是预定义布局

While Creating Adapter for your Spinner give custom layout instead of predefined one

创建名为 spinner_row.xml 的 xml

Create xml named spinner_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/cust_view"  
    android:layout_width="match_parent" 
    android:textColor="@color/black"
    android:textSize="12dp"
    android:layout_height="36dp" 
    android:gravity="left|center_vertical"/> 

在这里你可以通过修改这个textview来改变微调器中元素的颜色文本大小和宽度和高度

Here you can change the color Text size and width and height of the Elements in the spinner by modifying this textview

在创建Adapter时像这样使用

Use it like this while creating Adapter

 ArrayAdapter<String> adapter=new ArrayAdapter<String>(context, R.layout.spinner_row,yourlist);

最后一个任务是例行的

spinner.setAdapter(adapter);

希望对你有所帮助.

这篇关于如何更改 Android 上打开的 Spinner 的文本背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆