无法更改微调器中项目的颜色

时间:2014-12-23 16:22:57

标签: android spinner listitem

我在代码中添加了两个微调器。但是默认情况下,微调器的项目显示为黑色。我想更改微调项目的颜色。我已在simple_spinner_item.xml文件中添加颜色。但颜色没有改变。我在这里贴了我的代码.plz帮帮我...

 public class Alert_activity extends Activity implements OnItemSelectedListener{
Spinner s1,s2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_activity);
        s1 = (Spinner)findViewById(R.id.spinner1);
        s2 = (Spinner)findViewById(R.id.spinner2);
        s1.setOnItemSelectedListener(this);
    }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        String sp1= String.valueOf(s1.getSelectedItem());
         if(sp1.contentEquals("Events")) {
            List<String> list = new ArrayList<String>();
            list.add("Heavy Rain(mm/hr)");
            list.add("Smoke/Fog");
            list.add("Heat/Cold Wave");
            list.add("Strong Winds");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dataAdapter.notifyDataSetChanged();
            s2.setAdapter(dataAdapter);
            s2.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapter, View v,
                        int position, long id) {
                    // On selecting a spinner item
                    String item = adapter.getItemAtPosition(position).toString();
                                    Log.d("Item",item);
                     FragmentTransaction tx = getFragmentManager() .beginTransaction();
                     if(item=="Heavy Rain(mm/hr)")
                    {
                        tx.replace(android.R.id.content, new heavyrain());
                                    tx.commit();
                                 }
            });
        }
        if(sp1.contentEquals("Forecast")) {
            List<String> list = new ArrayList<String>();
            list.add("Heavy Rain(mm/hr)");
            list.add("Cyclone");
            list.add("Cloudburst");
            ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
            dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dataAdapter2.notifyDataSetChanged();
            s2.setAdapter(dataAdapter2);
            s2.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> adapter, View v,
                        int position, long id) {
                            String item = adapter.getItemAtPosition(position).toString();
                    Log.d("Inside loop","inside loop");
                         FragmentTransaction tx = getFragmentManager() .beginTransaction();
                                 if(item=="Cyclone")
                    {
                        tx.replace(android.R.id.content, new Cyclone());
                    }       
                            else if(item=="Heavy Rain(mm/hr)")
                    {
                        tx.replace(android.R.id.content, new forecast_heavyrain());

                               }
                    else if(item=="Cloudburst")
                    {
                        tx.replace(android.R.id.content, new forecast_cloudburst());
                    }
                    tx.commit();
                }

                            });
}
}
simple_spinner_item.xml 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textAlignment="inherit"
    android:textColor="#FFFFFF"/>

2 个答案:

答案 0 :(得分:0)

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener(){     public void onItemSelected(AdapterView parent,View view,int pos,             长id){

    ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
    ((TextView) parent.getChildAt(0)).setTextSize(5);



}

public void onNothingSelected(AdapterView<?> parent) {

}

};

或参考此链接:How to change spinner text size and text color?

答案 1 :(得分:0)

如果您的simple_spinner_item.xml包含您的微调器项目的背景颜色,请使用

dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item); 而不是dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

同样,

dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item);代替dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

您可能还需要将ArrayAdapter中的布局设置更改为自定义布局以解决问题。