造型的Andr​​oid搜索视图和动作条下拉列表视图、造型、动作、列表

2023-09-12 02:40:49 作者:Cold mood(冷情绪)

在我的应用程序,我有一个显示搜索建议一个搜索查看。我想下拉/微调和文字是一种特定的颜色。目前,我已经能够做出的唯一改变是输入的文本通过下列文字颜色:

In my app, I have a searchview that displays search suggestions. I want the dropdown/spinner and text to be a particular color. Currently the only change I've been able to make is the text color of the inputted text via the following:

   <style name="MyApp.AutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
    <item name="android:textColor">@color/white</item>
    <item name="android:textCursorDrawable">@null</item>
    <item name="android:background">@color/myBackgroundColor</item>
  </style>

该应用程序目前显示搜索查看的结果是这样的:

The app currently displays the searchview results like this:

我想知道的是,我将如何去改变的部分,如searchHint颜色,下拉背景和下拉项文本的颜色?

What I would like to know is how would I go about changing parts like the searchHint color, the drop down background and the drop down item text color?

我定位API 19 +

I am targeting API 19+

推荐答案

于是经过大量的搜索,最接近的解决方案,我可以找到这个如下(感谢的 GZ95的回答):

So after a lot of searching, the closest solution I can find to this is as follows (thanks to GZ95's answer!):

SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
//Set the input text color
autoComplete.setTextColor(Color.WHITE);
// set the hint text color
autoComplete.setHintTextColor(Color.WHITE);
//Some drawable (e.g. from xml)
autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);

这不是最优雅的,但它已经为我工作。唯一的问题我有突出的是如何改变建议项文本的颜色。我欢迎听到更优的解决这个问题。

It's not the most elegant but it has worked for me. The only question I have outstanding is how to change the suggested item text color. I am welcome to hear more optimal solutions to this problem