15

我看到很多关于如何更改微调器文本颜色的主题,但我不明白如何使用

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代码中真正做什么:?任何回复都将不胜感激 请尽可能详细地回答清楚

4

8 回答 8

21

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

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" />
于 2013-08-20T09:53:36.833 回答
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..
于 2013-08-19T11:09:11.473 回答
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" />
于 2014-07-10T06:07:20.123 回答
2

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

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) {

        }
    });
于 2016-05-09T11:49:18.890 回答
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) 
 {
 }
});
于 2013-08-19T11:11:46.390 回答
0

我这样做如下:我使用了getDropDownView()andgetView()方法。

用于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;
}
于 2015-12-14T10:27:45.123 回答
0

从 API 级别 16 及以上,您可以使用以下代码更改微调器中的下拉图标。只需转到 setonItemSelectedListener 中的 onItemSelected 并更改这样选择的 textview 的可绘制对象。

 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) {
           }
        });

希望它会帮助某人。

于 2016-10-10T08:03:24.310 回答
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>
于 2017-06-05T13:22:06.763 回答