更改特定项目的微调器背景颜色

时间:2016-05-13 19:29:23

标签: java android

我有一个问题。是否可以为特定项目设置特定的背景颜色? 。例如,我想为项目“DLUGOSC”设置“黄色”。下面是我的strings.xml和微调器布局。 感谢

**strings.xml**
<resources>
<string name="app_name">Konwerter Jednostek</string>
<string name="action_settings">Settings</string>

<string-array name="units">
    <item><b>DLUGOSC</b></item>
    <item>Cal</item>
    <item>Centymetr</item>
    <item>Stopa</item>
    <item>Jard</item>
    <item>Metr</item>
    <item>Mila</item>
    <item>Kilometr</item>
    <item>Decymetr</item>
    <item>Milimetr</item>

</string-array>

微调器布局

<Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner_from"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        />

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。在这里,我强调“Cal”。

List<String> units =   Arrays.asList(getResources().getStringArray(R.array.units));
    Spinner spinner = (Spinner) findViewById(R.id.spinner_from);
    spinner.setAdapter(new TestAdapter(units , this);



public class TestAdapter extends BaseAdapter {

List<String> strings;
Context context;

public TestAdapter(List<String> stringList, Context context) {
    strings = stringList;
    this.context = context;
}


@Override
public int getCount() {
    return strings.size();
}

@Override
public String getItem(int position) {
    return strings.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = new TextView(context);

    textView.setText(strings.get(position));

    //here you can use position or string
    if(position == 1 || strings.get(position).equals("Cal")) {
        textView.setBackgroundColor(Color.RED);
    }

    return textView;
}