2016-05-13 6 views
0

質問があります。特定の項目に特定の背景色を設定することはできますか? 。たとえば、 "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" 
     /> 

答えて

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; 
} 
関連する問題