2013-01-28 3 views
0

Я хочу изменить цвет элементов в spinner, чтобы сказать 3 разных цвета, и все элементы следует добавить динамически. Я добавляю элементы в список счетчиков через список. Предположим, у меня есть 2 списка, и я хочу объединить оба элемента списка в одном элементе прядильника, но оба должны быть разных цветов. Мой файл XML является:Изменить цвет элементов в spinner

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" >` 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:background="#66CCFF" 
    android:gravity="center" 
    android:text="abcd" 
    android:textColor="@android:color/white" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/tvMaterial" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/material" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 


<Spinner 
    android:id="@+id/spinMaterial" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    /> 

<TextView 
    android:id="@+id/tvWeight" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Weight" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/etWeight" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:hint="Enter Weight" 
    > 
</EditText> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/bAddCat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Add" /> 

    <Button 
     android:id="@+id/bCancelCat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Cancel" /> 

</LinearLayout>  
</LinearLayout> 

и Java класс

package com.androidui; 
    import java.util.ArrayList; 
    import java.util.List; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.text.Editable; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.Spinner; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class AddCategory extends Activity { 

TextView tv1; 
String cat = null; 
Spinner spin; 
Button add; 
Button cancel; 
EditText etWt; 
@Override 

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add_category); 

    tv1 = (TextView)findViewById(R.id.textView1); 
    add = (Button)findViewById(R.id.bAddCat); 
    cancel = (Button)findViewById(R.id.bCancelCat); 
    etWt = (EditText)findViewById(R.id.etWeight); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     cat = extras.getString("name"); 
     tv1.setText(cat); 
    } 

    addItemsOnSpinner(); 

    //Onclick listners to buttons 
    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(AddCategory.this, 
        "Material : "+ spin.getSelectedItem().toString() + "\nWeight : " + etWt.getText().toString(), 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 

    cancel.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
     } 
    }); 
} 

public void addItemsOnSpinner() { 

    spin = (Spinner) findViewById(R.id.spinMaterial); 
    List<String> list = new ArrayList<String>(); 
    list.add("Material 1"); 
    list.add("Material 2"); 
    list.add("Material 3"); 
    list.add("Material 4"); 
    list.add("Material 5"); 
    list.add("Material 6"); 

    //Second List 
    List<String> list2 = new ArrayList<String>(); 
    list2.add("Mat 7"); 
    list2.add("Mat 8"); 
    list2.add("Mat 9"); 

    //Combined List 
    List<String> listCombine = new ArrayList<String>(); 
    listCombine.addAll(list); 
    listCombine.addAll(list2); 

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_spinner_item, listCombine); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


    spin.setAdapter(dataAdapter); 
    } 

    } 

Просьба предложить метод. Thanx

+0

Вы должны написать свой собственный класс простирается от ArrayAdapter. Затем измените метод getView() по своему усмотрению. – Thommy

+0

вы можете использовать файл макета как элементы spinner ..., чтобы вы могли добавлять свои собственные цветовые форматы. – itsrajesh4uguys

ответ

Смежные вопросы