1

我正在创建一个搜索小部件和一个可搜索的活动。到目前为止,我遵循了 android 开发人员指南。

这是我的searchableActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

        String query = intent.getStringExtra(SearchManager.QUERY);
        doMySearch(query);
    }
}

如前所述, R.layout.search 正在创建错误。我在布局中没有搜索 xml,我不明白我应该在 search.xml 中定义什么。

4

2 回答 2

1

您需要在 res/layout 文件夹中有一个名为 search.xml 的布局,您将在其中显示搜索结果。在我的情况下,我在 RecyclerView 中显示了搜索结果,这是我的示例布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_background_search">

    <LinearLayout
        android:id="@+id/empty_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:tint="@color/color_primary_light"
            android:src="@drawable/ic_action_search" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/empty_search_result"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/search_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>
于 2016-02-11T19:09:53.970 回答
0

您正在尝试将当前布局设置为 search.xml,并且您自己说您没有此文件。

setContentView 正在设置您在此实例中运行应用程序时将看到的布局。

于 2014-09-21T02:07:53.720 回答