在操作栏上添加编辑框

6

我想知道如何实现这个功能?

我在操作栏中有一个搜索图标,如下图所示:

enter image description here

当点击它时,我希望操作栏变为文本编辑框,并在文本编辑框旁边有一个搜索图标。

我知道如何制作带有图片的文本编辑框,但是如何像下面的图片一样将文本编辑框放在操作栏上呢?

enter image description here

3个回答

8
你要找的是称为SearchView或Search Widget的东西。 在这里查看

2
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);

LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);

actionBar.setCustomView(v);

使用EditText创建搜索布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:focusable="true" >



    <EditText
        android:id="@+id/search_query"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:background="@drawable/bg_search_edit_text"
        android:imeOptions="actionSearch"
        android:inputType="text" />


</LinearLayout>

希望这能有所帮助。

我需要使用意图吗?假设搜索图标位于Activity A中,当按下时,它会变成EditText,并且我想在下面显示相关数据。 - Hoo
1
是的。这里有你想要的东西:http://www.androidhive.info/2013/11/android-working-with-action-bar/。 - Krishna Satwaji Khandagale

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接