如何将搜索添加到操作栏?

时间:2016-02-17 03:43:01

标签: android xml android-layout

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="#E0E0E0"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="25dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay" />



</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_cantos" />

</android.support.design.widget.CoordinatorLayout>

这是我的Java:

public class Cantos extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cantos);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.mipmap.ic_launcher);
    toolbar.setTitle("Cantos");
    toolbar.setTitleTextColor(getColor(R.color.colorPrimary));

}

}

我想知道如何将搜索添加到我的操作栏。所以当我输入字符时,会出现名字/对象。先感谢您。 (我的XML文件也是正确的吗?)

2 个答案:

答案 0 :(得分:4)

在Android中添加搜索视图实际上非常简单,因为它内置了对它的支持。浏览android提供的搜索视图功能以获取更多详细信息。 http://developer.android.com/guide/topics/search/search-dialog.html

答案 1 :(得分:0)

访问以下链接,您将找到在操作栏中实施searchview的最简单方法 http://javapapers.com/android/android-searchview-action-bar-tutorial/

相关问题