简体   繁体   English

Android:微调器,带有白色背景上的白色文本

[英]Android: spinner with white text on white background

I have some spinners in my app, which is relatively simple, but for some reason the text on the spinners is white when the background of the app is white. 我的应用程序中有一些微调器,这相对简单,但是由于某些原因,当应用程序的背景为白色时,微调器上的文本为白色。 The theme I'm trying to use is android:Theme.Light. 我要使用的主题是android:Theme.Light。

Spinner declaration in XML looks like this: XML中的微调框声明如下所示:

        <Spinner
            android:id="@+id/selection_spinner"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:paddingBottom="4dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="4dp"
             />

For the spinner declaration, I'm doing this: 对于微调器声明,我正在这样做:

destinationsSpinnerAdapter = new ArrayAdapter<Destination>(getActivity(), android.R.layout.simple_spinner_item);

Finally, I tried to follow the suggestions on this post but without success. 最后,我尝试遵循这篇文章的建议,但没有成功。

Try changing 'thisActivity()' to the 'this' pointer in your activity. 尝试在活动中将“ thisActivity()”更改为“ this”指针。 ie change 即改变

destinationsSpinnerAdapter = new ArrayAdapter<Destination>(getActivity(), android.R.layout.simple_spinner_item);

to

destinationsSpinnerAdapter = new ArrayAdapter<Destination>(this, android.R.layout.simple_spinner_item);

It worked for me. 它为我工作。

Try this: 尝试这个:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    // TODO Auto-generated method stub
    ((TextView) parent.getChildAt(0)).setTextColor(Color.MAGENTA);
    ((TextView) parent.getChildAt(0)).setTextSize(12);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

   }
});

If you really get stuck, you can create a custom ArrayAdapter . 如果确实遇到问题,可以创建一个自定义ArrayAdapter The example below uses white colour but you can change it in SetColourWhite method (and rename the method ;) 下面的示例使用白色,但是您可以在SetColourWhite方法中进行更改(并重命名该方法;)

This type of adapter should work everywhere. 这种适配器应可在任何地方使用。 I had hard time to style a spinner in the action bar - this helped. 我很难在动作栏中设置微调框的样式-这很有帮助。

public class WhiteTextArrayAdapter<T> extends ArrayAdapter<T> {

    public WhiteTextArrayAdapter(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);
    }

    public static WhiteTextArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new WhiteTextArrayAdapter<CharSequence>(context, textViewResId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    private View SetColourWhite(View v) {
        if (v instanceof TextView) ((TextView)v).setTextColor(Color.rgb(0xff, 0xff, 0xff)); // white
        return v;
    }

}

Change your ArrayAdapter to have Android.R.Simple_List_item_1 将您的ArrayAdapter更改为具有Android.R.Simple_List_item_1

That won't fix your styling problem (check your values folders, you should have 3 style.XML documents) but it will solve your immediate problem. 那不会解决您的样式问题(检查您的values文件夹,您应该拥有3个style.XML文档),但是它将解决您的直接问题。

You use the following style for the Spinner. 您将以下样式用于微调框。 Use this in style.xml : 在style.xml中使用它:

<style name="SpinnerThemeLight" >
<item name="android:colorBackground">@color/white</item>
<item name="android:textColorPrimary">@color/black</item>
<item name="android:textColorPrimaryDisableOnly">@color/black</item>
</style>

if you have already created a style, add the only this item into it: 如果您已经创建了样式,请在其中添加唯一的一项:

<item name="android:textColorPrimaryDisableOnly">@color/black</item>

Thanks. 谢谢。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM