如何创建一个简化的搜索界面?
How to create a simplified search interface?
我想在 ListView 上实现搜索,它应该是这样的(Google 保留标签界面):
- 没有删除按钮
- 没有搜索图标
- 没有设置按钮
- 只有后退按钮
我应该吗?:
- 使用 EditText 实现布局,将其附加到 Action Bar 并从 ActionBar 中删除任何其他内容
- 创建自定义 SearchView(如果是,如何访问 SearchView 布局?)
- 以某种方式删除 Action Bar 并在主布局中添加 EditText
我认为最好删除 Action Bar 并添加一个 Toolbar。您可以将 EditText 添加到工具栏。所有搜索逻辑都可以在编辑文本 TextChangedListener 上完成。
可以通过在 styles.xml
中更改为 NoActionBar 来删除操作栏
工具栏提供了很大的自定义空间
首先为操作栏制作自定义布局(为简单起见,仅使用 EditText):
custom_action_bar_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editText" android:inputType="text"/>
</LinearLayout>
添加到Activity
:
// hide icon
getActionBar().setDisplayShowHomeEnabled(false);
// hide title
getActionBar().setDisplayShowTitleEnabled(false);
// set custom layout
getActionBar().setCustomView(R.layout.custom_action_bar_layout);
// enable custom view to be shown
getActionBar().setDisplayShowCustomEnabled(true);
// access to custon veiw element
EditText text = (EditText) findViewById(R.id.editText);
我想在 ListView 上实现搜索,它应该是这样的(Google 保留标签界面):
- 没有删除按钮
- 没有搜索图标
- 没有设置按钮
- 只有后退按钮
我应该吗?:
- 使用 EditText 实现布局,将其附加到 Action Bar 并从 ActionBar 中删除任何其他内容
- 创建自定义 SearchView(如果是,如何访问 SearchView 布局?)
- 以某种方式删除 Action Bar 并在主布局中添加 EditText
我认为最好删除 Action Bar 并添加一个 Toolbar。您可以将 EditText 添加到工具栏。所有搜索逻辑都可以在编辑文本 TextChangedListener 上完成。
可以通过在 styles.xml
中更改为 NoActionBar 来删除操作栏工具栏提供了很大的自定义空间
首先为操作栏制作自定义布局(为简单起见,仅使用 EditText):
custom_action_bar_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editText" android:inputType="text"/>
</LinearLayout>
添加到Activity
:
// hide icon
getActionBar().setDisplayShowHomeEnabled(false);
// hide title
getActionBar().setDisplayShowTitleEnabled(false);
// set custom layout
getActionBar().setCustomView(R.layout.custom_action_bar_layout);
// enable custom view to be shown
getActionBar().setDisplayShowCustomEnabled(true);
// access to custon veiw element
EditText text = (EditText) findViewById(R.id.editText);