如何在像谷歌这样的工具栏上有一个searchview?

时间:2017-12-20 18:20:23

标签: android kotlin

我不在搜索视图中查找任何功能,在工具栏中,处理点击以便将其定向到搜索活动。

这是我想要的工具栏:

enter image description here

这将始终展开,点击将定向到另一个活动。以下是代码:

XML ToolBar主页:

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<!--                <SearchView
                    android:id="@+id/search"
                    shapeColor="@color/white"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="@color/white"
                    android:onClick="@{(view) -> viewModel.onSearchClicked(view)}"
                    android:queryHint="Pesquisar"
                    android:searchIcon="@drawable/ic_search_24dp"
                    android:voiceIcon="@drawable/ic_settings_voice_black_24px"
                    app:iconifiedByDefault="false">
//TODO doesn't work
                </SearchView>-->

            </android.support.v7.widget.Toolbar>

菜单主页:

<item
    android:id="@+id/item_search"
    android:orderInCategory="200"
    android:title="@string/search"
    android:icon="@drawable/ic_search_24dp"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"
    android:iconifiedByDefault="false" />

HomeActivity:

    override val contentView = R.layout.view_home

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_home, menu)

        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
/*        R.id.item_search -> {
            startActivity(Intent(this, SearchActivity::class.java))
            true
        }*/
        R.id.item_zuk_code -> {
            givenPermission(android.Manifest.permission.CAMERA, granted = {
                startActivity(Intent(this@HomeActivity, ZukCodeReaderActivity::class.java))
            })
            true
        }
//        R.id.item_contacts -> {
//            startActivity(Intent(this, ContactsSuggestionActivity::class.java))
//            true
//        }
        else -> super.onOptionsItemSelected(item)
    }

修改

HomeActivity菜单:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu_home, menu)

    val manager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val search = menu.findItem(R.id.item_search).actionView as android.support.v7.widget.SearchView
    val si = manager.getSearchableInfo(componentName)

    var opt : Int = search.imeOptions
    search.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
    search.backgroundTintMode
    search.setSearchableInfo(si)
    search.setIconifiedByDefault(false)

    return super.onCreateOptionsMenu(menu)
}

我有什么:

enter image description here

0 个答案:

没有答案