简体   繁体   English

微调框上的文字是白色背景上的白色

[英]Text on spinner is white on a white background

The text on my spinners is white, and I have no idea why. 微调框上的文字是白色的,我不知道为什么。

在此处输入图片说明

This is my xml, nothing special 这是我的xml,没什么特别的

 <Spinner
     android:id="@+id/spinner_date"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1" />

And my code 还有我的代码

dateSpinner = (Spinner) findViewById(R.id.spinner_date);
selectedDate = calendar.getTime();
List<String> list = new ArrayList<String>();
list.add(formatter.format(selectedDate));
dateAdapter = new ArrayAdapter<String>(mContext,
            android.R.layout.simple_spinner_item, list);
dateSpinner.setAdapter(dateAdapter);

What could be the reason that my text is displayed in white? 我的文字显示为白色可能是什么原因?

EDIT: I've found the reason, I replaced the mContext parameter which was set in my onCreate. 编辑:我已经找到了原因,我替换了在onCreate中设置的mContext参数。

mContext = getApplicationContext();

Now I use d 现在我用d

ateAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);

and it works. 而且有效。

I have same problem and have found the answer. 我有同样的问题,并且找到了答案。 You dont use application context, instead, just use getActivity() (if you are in fragment) or this (if you are in activity), it will work 您不使用应用程序上下文,而是使用getActivity() (如果您处于片段状态)或this(如果您处于活动状态),它将起作用

 dateAdapter = new ArrayAdapter<String>(**this**,
        android.R.layout.simple_spinner_item, list);

I solved this problem using 我使用解决了这个问题

getBaseContext()

instead of 代替

getApplicationContext()

i change it from 我从改变

new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, some_list);

to new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); 到新的ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);

it's fixed, although i don't want to use "this" 它是固定的,尽管我不想使用“ this”

I also had same problem it was because of my application theme. 我也有同样的问题,这是因为我的应用程序主题。 Which I solved by replacing the 我通过替换

android.R.layout.simple_spinner_item

with

android.R.layout.simple_list_item_1

in my ArrayAdapter . 在我的ArrayAdapter I hope this may solve your problem 我希望这可以解决您的问题

Maybe you have a white android:textColor="@android:color/white" attribute in your simple_spinner_item.xml in the layout folder of your project. 在项目的布局文件夹中的simple_spinner_item.xml中,也许您有一个白色的android:textColor="@android:color/white"属性。

Better use a custom spinner item layout with a good android:textColor="@android:color/COLOR_YOU_WANT_TO_USE" attribute. 最好使用具有良好android:textColor="@android:color/COLOR_YOU_WANT_TO_USE"属性的自定义微调器项目布局。

You can easily style the Spinner. 您可以轻松设置微调框的样式。 Use this in style.xml : style.xml使用它:

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

In the above xml file, I have simply given black color for time sake. 在上面的xml文件中,出于时间考虑,我仅给出了black Just play with it and sort out your preferred color. 只需玩一下,然后挑选出您喜欢的颜色即可。

Define the Spinner in the activity.xml as follows : 如下在activity.xml定义Spinner

 <android.support.v7.widget.AppCompatSpinner
   android:id="@+id/spinner"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:entries="@array/countrys"
   android:spinnerMode="dropdown"
   android:theme="@style/SpinnerThemeLight"/>

I've changed the text color of that spinner's textview without creating a new layout. 我已经更改了微调框的textview的文本颜色,而没有创建新的布局。 I know it's been a long time but it's what worked for me, just thought of sharing it. 我知道已经很长时间了,但这对我有用,只是想分享一下。 Best part is you can use it on any default adapter. 最好的部分是您可以在任何默认适配器上使用它。

Here's the code : (this for Activity & requireActivity for Fragments) 这是代码:(这是Activity的,而requireActivity是片段的)

1) Java 1)Java

arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,groups){
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView listItem = view.findViewById(android.R.id.text1);
            listItem.setTextColor(Color.WHITE);
            listItem.setTextSize(20);
            listItem.setElevation(18);
            return view;
        }
    };

2) Kotlin 2)科特林

arrayAdapter = object : ArrayAdapter<String>(requireActivity(), android.R.layout.simple_spinner_item, spinnerCategoriesList) {
        override fun getView(position: Int, @Nullable convertView: View?, parent: ViewGroup): View {
            val view = super.getView(position, convertView, parent)
            val listItem = view.findViewById<TextView>(android.R.id.text1)
            listItem.setTextColor(Color.BLACK)
            listItem.textSize = 16f
            return view
        }
    }

I assume you have created your own TextView for your Spinner, eg 我假设您已经为Spinner创建了自己的TextView,例如

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="@color/white"
    android:padding="5dip"
    />

and glued it to your Adapter via a call like that 并通过这样的调用将其粘贴到您的适配器

String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);

This should result in having the selection-text of your spinner including its items within the dropdown view painted in white. 这应该导致微调框的选择文本(包括下拉菜单中的项目)涂成白色。 Now the background of your dropdown view will be influenced by your app theme, which in most cases, will lead your white text to be rendered on a white background. 现在,下拉视图的背景将受您的应用程序主题的影响,在大多数情况下,这将导致白色文本在白色背景上呈现。 To avoid this android allows you to set a resource for your spinner dropdown view. 为了避免这种情况,Android允许您为微调框下拉菜单设置资源。 You can set your own view or simply use the default drop-down view, which will overwrite your custom textview within the dropdown menu through the call 您可以设置自己的视图,也可以只使用默认的下拉视图,这将通过调用覆盖下拉菜单中的自定义文本视图

 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

The complete code could look like this 完整的代码如下所示

this.spinner = findViewById(R.id.spinnerView);
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(adapter);

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

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