我想更改微调器中项目的颜色,说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);
}
}
请建议一种方法。 感谢名单
答案 0 :(得分:0)
您可以尝试使用SpannableString为文字着色http://developer.android.com/reference/android/text/SpannableString.html