2016-05-13 153 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; 
}