TextView Color in the Spinner

2019-04-17 11:14发布

问题:

What i want is the Text color of the spinner should be white at runtime when it is displayed inside the Spinner as it is in spinner 1 and 2 and the text color of the list of Spinner should be black as in the picture.

The problem is i am able to do it for the first 2 Spinners but fail to do it for the 3rd and the 4th Spinner.

If you can see the 3rd and 4th Spinner has values but in black. I don't understand that if its possible for 2 spinners then y not for all 4. please help me.

This is my code to change color at runtime:

public class Text                  // class for changing the text color of spinner
{
    private TextView tv = null;
    Text(View spin_adptr)
    {

        tv = (TextView) spin_adptr.findViewById(R.id.textspin);
        tv.setTextColor(Color.WHITE);
    }
}

and this is how i am calling it:

public void onItemSelected(AdapterView<?> parent, View arg1, int position,
        long arg3) {
    int id = parent.getId();
    Text text = new Text(spinner1);   //Change of color (Working)
            Text text1 = new Text(spinner3);  //Change of Color(ERROR)
            Text text2 = new Text(spinner4);  //Change of Color(ERROR)



    switch (id) {
    case R.id.spinner1:

        String b = spinner1.getSelectedItem().toString();

        if (b != null) {
            dbCarHelper.make_pop(b);
        }



        List<String> lb = h.getAllLabels();

        ArrayAdapter<String> dAdapter = new ArrayAdapter<String>(this,
                R.layout.spin_adptr,lb);

        dAdapter.setDropDownViewResource(R.layout.spin_adptr);
        spinner2.setAdapter(dAdapter);

        break;

    case R.id.spinner2:
        Text text1 = new Text(spinner2);   //// Change of color (working)
        String a = spinner2.getSelectedItem().toString();

        if (a != null) {
            dbCarHelper.m_pop(a);
        }

        CarHelper c = new CarHelper(getApplicationContext());

        List<String> lable = c.getAllLabel();

        ArrayAdapter<String> dAdaptr = new ArrayAdapter<String>(this,
                R.layout.spin_adptr,lable);

        dAdaptr.setDropDownViewResource(R.layout.spin_adptr);

        spinner3.setAdapter(dAdaptr);


        break;

    }

    }

回答1:

make a xml file named as spinner_layout and write code as below

  <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="4dp"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:ellipsize="marquee"
android:singleLine="true" />

make another xml named as spinner_dropdown and write below code

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="4dp"
android:textSize="18sp"
android:textColor="#000000"
android:ellipsize="marquee"
android:singleLine="true" />

now update your code as below

ArrayAdapter<String> dAdapter = new ArrayAdapter<String>(this,
            R.layout.spinner_layout,lb);

    dAdapter.setDropDownViewResource(R.layout.spinner_dropdown);