【问题标题】:How to set hint color for spinner? [duplicate]如何为微调器设置提示颜色? [复制]
【发布时间】:2017-12-14 06:22:13
【问题描述】:

我想为微调器中的提示设置颜色。请帮帮我...这是我的微调器。

    List<String> catagories = new ArrayList<>();
    catagories.add("General");
    catagories.add("OBC");
    catagories.add("SC/ST");

    Spinneradapter adapter = new 
         Spinneradapter(this,android.R.layout.simple_spinner_dropdown_item);
    adapter.addAll(catagories);
    adapter.add("Select Category");
    spinner.setAdapter(adapter);
    spinner.setSelection(adapter.getCount());

【问题讨论】:

标签: android colors spinner adapter


【解决方案1】:

为您的微调器项目创建自定义 XML 文件。

spinner_item.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="20sp"
    android:gravity="left"  
    android:textColor="#FF0000"         
    android:padding="5dip"
    />

现在使用此文件显示您的微调器项目,例如:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);

您不需要设置下拉资源。 spinner_item.xml 只会在微调器中显示您的项目。

【讨论】:

  • 颜色已更改,但所有选定项目的颜色也已更改。我只想更改提示颜色。
【解决方案2】:

SpinnerAdaptergetView() 中,您应该添加这些行来设置 0 索引上项目的文本颜色:

        TextView names = (TextView) view.findViewById(R.id.textView);
        names.setText(dataList[i]);

        if (i == 0) {
            // Set the hint text color gray
            names.setTextColor(context.getResources().getColor(R.color.color_hint));
        } else {
            names.setTextColor(context.getResources().getColor(R.color.color_text));
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多