如何在Android中设置自定义搜索建议列表的样式

时间:2015-06-16 04:01:24

标签: android android-actionbar searchview

我根据this tutorial实施了内容提供商的自定义建议。它有效,但是我对建议列表造型有问题。我目前的建议清单有黑色背景和白色文字。我想把它改成白色背景和黑色文字。我在网上做了一些搜索,并在styles.xml文件中尝试了类似的东西:

class TestModule : Ninject.Modules.NinjectModule
{
    public override void Load()
    {
        for (int i=0; i<100; i++)
        {
            Bind<IWeapon>().To<Shuriken>();
        }
    }
}

我的list_item_search_suggestion看起来像这样:

<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="suggestionRowLayout">@layout/list_item_search_suggestion</item>
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>

然而,当我运行应用程序并搜索某些内容时,它会在建议列表显示之前崩溃。我从SuggestionsAdapter中的newView方法得到一个NullPointerException。这个SuggestionAdapter来自SDK,我没有实现自己的适配器。我只是实现内容提供程序并在searchable.xml中设置它。堆栈轨道也是这样的:

    <?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@android:id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:minHeight="56dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@android:color/black"
    tools:text="Blah blah blah"/>

1 个答案:

答案 0 :(得分:2)

我想出来了。基本上,SuggestionsAdapter引用了我在自定义建议行布局中未提供的一些小部件。所以我从源代码复制了默认的自定义建议行布局,并将背景和文本颜色更改为我的自定义布局。它现在正在工作。