Spinner中的TextView颜色

时间:2013-04-19 09:45:38

标签: android textview android-spinner

我想要的是旋转器的文本颜色在运行时在旋转器中显示时应该是白色的,因为它在旋转器1和2中,并且旋转器列表的文本颜色应该是黑色,如图所示。

问题是我可以为前2个Spinners做这个但是没能为第3个和第4个Spinner做这个。

如果你可以看到第3和第4个微调器有值但是黑色。我不明白,如果它可能是2个旋转器,那么你不是全部4.请帮助我。

enter image description here enter image description here

这是我在运行时改变颜色的代码:

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

这就是我所说的:

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 个答案:

答案 0 :(得分:5)

创建一个名为spinner_layout的xml文件,并按如下所示编写代码

  <?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" />

创建另一个名为spinner_dropdown的xml并写下代码

 <?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" />

现在更新您的代码,如下所示

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

    dAdapter.setDropDownViewResource(R.layout.spinner_dropdown);