当前位置: 首页 > 知识库问答 >
问题:

在旋转器中更改选定项的颜色

祝俊
2023-03-14

出于某种原因,我的旋转器中选定项的文本颜色为白色。以下是活动的代码:

spinner = (Spinner) findViewById(R.id.spinner_categories);
arr_list_categories = manager.get_all_categories();
arr_list_categories_names = new ArrayList<String>();
// Loop through the Categories array
for (int i = 0; i < arr_list_categories.size(); i++) 
{
    Category curr_category = arr_list_categories.get(i); // Get the Category object at current position
    arr_list_categories_names.add(curr_category.getCategory_name()
            .toString()); // Add the name of the Category to the Names Array        
}
// Setting the Adapter, to display retrieved data in the Spinner
adapter=new ArrayAdapter<String> (this, R.layout.simple_spinner_item, arr_list_categories_names);
// Setting the display source for the Spinner View
adapter.setDropDownViewResource(R.layout.simple_spinner_item);
// Populating the Spinner
spinner.setAdapter(adapter);
// Registering for On Item Selected listener
spinner.setOnItemSelectedListener(spinnerListener);

下面是simple_spinner_item的XML:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:padding="5dip"
    android:textColor="#FF0000"
    android:textSize="20sp" />

下拉项显示为红色,但不显示所选项。我试图将XML中的textColor更改为selector:Android:textColor=“@drawable/spinner_text_color_selector”并创建了这个选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#FF0000"/><!-- pressed -->
    <item android:state_focused="true" android:color="#FF0000"/><!-- focused -->
    <item android:state_selected="true"  android:color="#FF0000"/>
    <item android:state_activated="true" android:color="#FF0000"/>
    <item android:color="#FF0000"/><!-- default -->
</selector>

但是所选项目的文本颜色仍然是白色。所以我尝试在OnItemSelectedListener中这样做:

TextView selected_tv = (TextView) parent.getChildAt(0);
selected_tv.setTextColor(Color.BLUE);

还是白色的...

为什么会发生这种情况,我如何修复它?谢谢!

共有1个答案

麻书
2023-03-14
TextView selected_tv = (TextView) parent.getChildAt(0);
selected_tv.setTextColor(Color.BLUE);

在此代码中,在第一行进行如下更改(使用TextView的id):

TextView selected_tv = (TextView) parent.findViewById(R.id.selected_tv);
selected_tv.setTextColor(Color.BLUE);
 类似资料:
  • 我正试图改变Android中旋转器按钮的颜色,但我无法让它工作。有人能帮忙吗?

  • 我正在试着对所选项目在旋转器中的定位做一个轻微的调整。不是下拉列表项,因为我在适配器中已经有了一个自定义视图,而是特定的选定项。 正如你在截图中看到的,“任何”是当前选择的项。但它在容器中的对齐很奇怪,因为它必须容纳下拉列表中最长的字符串,即“Dark Purple Burnt Sienna”(或其他)。我想将选定的文本向右对齐,这样“任何”就在下拉指示器旁边,而不是中间的出路。 我尝试对我的自定

  • 问题内容: 我想将matplotlib的标准颜色旋转更改为另一个颜色图。具体来说,我想使用“ gdist_rainbow”。那有可能吗,如果可以的话,我该如何实现呢? 我已经有自定义设置,例如 所以我想我只是在寻找要添加的正确键。 问题答案: 您需要为rcParameter提供一个颜色循环。一个颜色循环由颜色列表组成。可以根据颜色图进行选择。请参见下面的示例:

  • 问题内容: 我有一个选择框。通过已引用的CSS文件,已使用不同的颜色来设置选项的样式。我希望能够选择一个选项,然后将关闭的选择框的文本颜色更改为所选选项的颜色。 因此,如果我选择“香蕉”,则文本应从黄色变为红色。 我努力了: 但这仅在我在html文档内的选项标签中定义样式时才有效。 我也尝试过JavaScript: 但这总是返回颜色:白色。getStyle函数在我使用过的其他任何地方都可以使用,因

  • 我正在使用JTextPane创建一个文本编辑器,它允许用户更改所选文本的颜色。但是当用户选择文本时,然后选择更改颜色的选项(比如,改为红色),直到取消选择文本时,文本才会显示为红色。我尝试使用setSelectedTextColor来更改所选文本的颜色,但这不起作用,因为当之后选择文本时,它会将文本更改为红色。有没有一种方法可以使选定的文本显示为它的实际颜色?或者像它在Word中的工作方式那样,它

  • 我正在使用带有的,我想知道如何最有效地更改TabLayout中选定选项卡的图标的颜色。 谷歌的Youtube应用程序是如何实现这一点的完美参考。在主页上,有四个深灰色图标。选择特定选项卡后,选项卡图标变为白色。 没有任何第三方库,我怎样才能达到同样的效果? 一个可能的解决方案显然是用选择器。但是在这种情况下,我必须找到图标的白色和灰色版本,然后在选项卡被选中或取消选中时切换图标。我想知道是否有一个