如何更改微调文本颜色

时间:2013-08-19 10:54:03

标签: android text colors spinner textcolor

我看到很多关于如何更改微调器文本颜色的主题,但我无法理解如何使用

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:singleLine="true"
    android:textColor="@color/iphone_text" />

我在java代码中应该做些什么:? 任何回复都是精心准备的 请尽可能详细地回答

8 个答案:

答案 0 :(得分:20)

对我来说,一个完整的答案是:

public class ee extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ww);
        addListenerOnSpinnerItemSelection();

    }

    public void addListenerOnSpinnerItemSelection() {
        ArrayList<String> array = new ArrayList<String>();
        array.add("item0");
        Spinner spinner1;
        ArrayAdapter<String> mAdapter;
        spinner1 = (Spinner) findViewById(R.id.spinner2);
        mAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, array);
        spinner1.setAdapter(mAdapter);
    }

}

并且在 res / layout添加新的xml文件:

(在spinner_item.xml中)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:singleLine="true"
    android:textColor="#00f0ff" />

答案 1 :(得分:10)

此处您必须在数组适配器中设置spinner_item.xml。在.java文件中添加这段代码

Spinner yourSpinner;
ArrayAdapter<String> yourAdapter;
yourSpinner= (Spinner) findViewById(R.id.yourSpinnerID);
yourSpinner.setAdapter(yourAdapter);
yourAdapter= new ArrayAdapter<String>(this, R.layout.spinner_item, value);
//here value is your items in spinner..

答案 2 :(得分:4)

Spinner spinner = (Spinner) findViewById(R.id.spinner);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,
                int arg2, long arg3) {

            ((TextView) arg0.getChildAt(0)).setTextColor(Color.RED);
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_row, list);
        spinner.setAdapter(adapter);

list_row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#000000"
android:textSize="20sp" />

答案 3 :(得分:1)

这是我为微调器文本执行的最简单的方法。我知道它迟到了,但它会帮助一些人。

select_gender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  // give the color which ever you want to give to spinner item in this line of code               
            ((TextView)parent.getChildAt(position)).setTextColor(Color.parseColor("#646b99"));
            spinner_selected_gender=gender_list.get(position);
            Toast.makeText(getApplicationContext(),"You selected"+spinner_selected_gender,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

答案 4 :(得分:0)

Spinner spnCategory= (Spinner)findViewById(R.id.my_spinner);
..

ArrayAdapter<String> adptSpnCategory = new ArrayAdapter<String>this,R.layout.custom_spinner_item, alCategoryName);
adptSpnCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnCategory.setAdapter(adptSpnCategory);
spnCategory.setOnItemSelectedListener(new OnItemSelectedListener() 
{
 public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
 {
 }
 public void onNothingSelected(AdapterView<?> arg0) 
 {
 }
});

答案 5 :(得分:0)

我完成了以下操作:我使用了getDropDownView()getView()方法。

使用getDropDownView()打开Spinner

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
     View view = convertView;
     if (view == null) {
        LayoutInflater vi = (LayoutInflater) activity
                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.context_row_icon, null);
     }
     TextView mTitle = (TextView) view.findViewById(R.id.context_label);
     ImageView flag = (ImageView) view.findViewById(R.id.context_icon);               

     mTitle.setText(values[position].getLabel(activity));

     if (!((LabelItem) getItem(position)).isEnabled()) {
          mTitle.setTextColor(activity.getResources().getColor(
               R.color.context_item_disabled));
     } else {
          mTitle.setTextColor(activity.getResources().getColor(
              R.color.context_item));
     }
     return view;
}

使用getView()封闭Spinner

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View view = convertView;
     if (view == null) {
         LayoutInflater vi = (LayoutInflater) activity
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         view = vi.inflate(R.layout.context_row_icon, null);
     }
     TextView mTitle = (TextView) view.findViewById(R.id.context_label);
     ImageView flag = (ImageView) view.findViewById(R.id.context_icon);

     mTitle.setText(values[position].getLabel(activity));

     mTitle.setTextColor(activity.getResources().getColor(
           R.color.context_item_disabled));

     return view;
}

答案 6 :(得分:0)

从API级别16及更高版本,您可以使用以下代码更改微调器中的下拉图标。只需转到setonItemSelectedListener中的onItemSelected,然后更改像这样选择的textview的drawable。

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      // give the color which ever you want to give to spinner item in this line of code

    //API Level 16 and above only.
            ((TextView)parent.getChildAt(position)).setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,ContextCompat.getDrawable(Activity.this,R.drawable.icon),null);

//Basically itis changing the drawable of textview, we have change the textview left drawable.
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
           }
        });
希望它会对某人有所帮助。

答案 7 :(得分:0)

建立在noobProgrammer的答案上,因为不推荐使用singleLine,所以spinner_item.xml的快速更新版本适用于正在为此寻找答案的任何人。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1">
</TextView>