java AutoCompleteTextView 背景/前景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11787057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:21:16  来源:igfitidea点击:

AutoCompleteTextView background/foreground color

javaandroidxml

提问by AHméd Net

I am working on AutoCompleteTextView.Its working fine but the dropdown text is always white text on white background. this picture explain my problem

我正在处理 AutoCompleteTextView。它工作正常,但下拉文本始终是白色背景上的白色文本。这张图片说明了我的问题

picture explain my problem

图片说明我的问题

enter image description here

在此处输入图片说明

XML:

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000"
    >
    <!-- <AutoCompleteTextView  -->
    <AutoCompleteTextView
        android:id="@+id/text"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:hint="Tapez votre 1texte"
        android:textColor="#000"        
        />
</LinearLayout>

Java:

爪哇:

view = (AutoCompleteTextView)findViewById(R.id.text);        
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
adapter.setDropDownViewResource(R.drawable.ic_action_search);//dd
view.setAdapter(adapter);

回答by Sam

If you want to change the look of the dropdown items change the XML layout you pass to the ArrayAdapter, (in your case this is android.R.layout.simple_dropdown_item_1line).

如果您想更改下拉项的外观,请更改您传递给 ArrayAdapter 的 XML 布局(在您的情况下为android.R.layout.simple_dropdown_item_1line)。

Lets make a new layout named my_list_item.xmlin res/layout:

让我们创建一个名为my_list_item.xmlin的新布局res/layout

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/dropDownItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:textAppearance="?android:attr/textAppearanceMediumInverse"
    android:textColor="#00f" />

This text is smaller, blue, and centered. I don't recommend using this layout, it's more to demonstrate that you can customize it.

此文本更小、蓝色且居中。我不推荐使用这种布局,更多的是证明你可以自定义它。

Now use this instead:

现在改用这个:

view = (AutoCompleteTextView)findViewById(R.id.text);        
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list_item, data);
view.setAdapter(adapter);

When you set attributes here (like the text color):

当您在此处设置属性时(如文本颜色):

<AutoCompleteTextView
    android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:hint="Tapez votre 1texte"
    android:textColor="#000"        
    />

You are only changing the box above the dropdown list (the box that you see before you interact with the AutoCompleteTextView). Hope that helps!

您只是更改下拉列表上方的框(您在与 AutoCompleteTextView 交互之前看到的框)。希望有帮助!

回答by thecodefather

This question might be old but I believe this is very important for me to share. I had been experiencing the same problem as OP but it is because I was using 'getApplicationContext()' in place of 'this'

这个问题可能很老,但我相信这对我来说非常重要。我遇到了与 OP 相同的问题,但这是因为我使用的是“getApplicationContext()”而不是“this”


Wrong


错误的

ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (getApplicationContext(), android.R.layout.simple_list_item_1, PLACES);


Correct


正确的

ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, PLACES);

回答by amol khedkar

I tried setting up the theme before setcontext, tried different resources parameter in arrayAdapter and tried different theme ,but nothing helped.

我尝试在 setcontext 之前设置主题,在 arrayAdapter 中尝试了不同的资源参数并尝试了不同的主题,但没有任何帮助。

Then I changed the context from 'this' to 'getApplicationContext' but the problem was persistent.

然后我将上下文从“this”更改为“getApplicationContext”,但问题仍然存在。

Finally I changed the context parameter to "getBaseContext()" and the problem was solved.

最后我将上下文参数更改为“getBaseContext()”,问题就解决了。

回答by Prashant Jaiswal

i got my solution in slecting layout of "select_dialog_singlechoiceArrayAdapter<String > s1= new ArrayAdapter<String> (this,android.R.layout.select_dialog_singlechoice,state);enter image description here

我在“select_dialog_singlechoice enter image description here”的选择布局中得到了我的解决方案ArrayAdapter<String > s1= new ArrayAdapter<String> (this,android.R.layout.select_dialog_singlechoice,state);

instead of get application context write "this" in ArrayAdapter<>;

在 ArrayAdapter<> 中写入“this”而不是获取应用程序上下文;

try using different layouts u will definately solve your queries

尝试使用不同的布局你肯定会解决你的查询

回答by Gowtham. R

I solved this issue by doing a simple tricky Way,

我通过一个简单的棘手方式解决了这个问题,

Just change the Theme of your activity declared in your AndroidManifest.xml as,

只需将您在 AndroidManifest.xml 中声明的活动主题更改为

 <activity
   android:name=".YourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:windowSoftInputMode="stateHidden"></activity>

and in the Activity as follows,

并在如下活动中

ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (getActivity(), android.R.layout.select_dialog_item, arraylist);

回答by Kanth

A tricky solution for this is: Add this line setTheme(android.R.style.Theme);before setContentView()in your activity file in which your autocompletetextview is present. Let me know if this works.

一个棘手的解决方案是:添加这一行 setTheme(android.R.style.Theme); setContentView()在您的自动完成文本视图所在的活动文件中之前。让我知道这个是否奏效。

回答by gustavomcastro

Answer by didldum https://code.google.com/p/android/issues/detail?id=5237

didldum 的回答https://code.google.com/p/android/issues/detail?id=5237

workaround by extending the theme and overriding the 2 styles for the typed text and the suggest text:

通过扩展主题并覆盖键入文本和建议文本的 2 种样式来解决方法:

  1. use an extended theme in your manifest: ... ...

  2. create the new theme (res/values/themes.xml) which uses fixed styles: ... @style/AutoCompleteTextViewLight @style/Widget.DropDownItemLight ...

  3. create the styles (res/values/styles.xml) which fix the color: ... @android:color/primary_text_light @android:color/primary_text_light

  1. 在清单中使用扩展主题: ... ...

  2. 创建使用固定样式的新主题 (res/values/themes.xml): ... @style/AutoCompleteTextViewLight @style/Widget.DropDownItemLight ...

  3. 创建固定颜色的样式(res/values/styles.xml): ... @android:color/primary_text_light @android:color/primary_text_light