我想将微调器中项目的颜色更改为 3 种不同的颜色,并且所有项目都应该动态添加。我正在通过列表在微调器中添加项目。假设我有 2 个列表,并且我想将两个列表项合并到单个微调器项中,但两者都应该是不同的颜色。我的 xml 文件是:
<?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);
}
}
请提出一种方法。谢谢