如何在Google Play商店应用中设置Android SearchView风格?

时间:2016-07-12 11:24:02

标签: android google-play themes android-appcompat searchview

我正在尝试在SearchView中设置toolbar的样式,使其外观/行为与SearchView中的Google Play Store app相似。它似乎将搜索视图包装在一个cardview中,但它似乎也整合了按钮/抽屉切换行为。

这是主要的活动searchView,它集成了抽屉切换

MainActivity SearchView

当您点击它时,抽屉切换更改为箭头(单击时将从搜索视图中删除焦点)

enter image description here

当您点击商店中的某个应用时,您会转到应用详细信息页面,并将搜索视图的图标化版本视为带有向上按钮的折叠操作项:

enter image description here

最后,如果你点击搜索图标,它会扩展为一个动作项目(带有一种涟漪动画),并在整个屏幕上显示并包含向上按钮:

enter image description here

当我尝试自己做的时候,我遇到了很多问题。我试图将searchview包装在cardview中并将其放在工具栏中。它有效,但左边总是有填充,我无法删除(我尝试过contentInsetStart,不能正常工作):

<android.support.v7.widget.Toolbar
    android:id="@+id/activityCatalogToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:contentInsetStart="0dp"
    app:contentInsetLeft="0dp">

    <android.support.v7.widget.CardView
        android:id="@+id/activityCatalogSearchContainer"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="end"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="8dp">

        <android.support.v7.widget.SearchView
            android:id="@+id/activityCatalogSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:queryHint=""
            android:iconifiedByDefault="false"/>

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

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

enter image description here

我还尝试删除工具栏并将其替换为FrameLayout。这允许我控制填充但显然我丢失了工具栏功能(向上按钮等)并且还引入了主题问题(图标和文本消失)。

有没有人知道他们是如何做到这一点的?我不想仅仅为框架中已存在的样式小部件添加另一个库。谢谢!

3 个答案:

答案 0 :(得分:10)

您可能会尝试深入研究这个项目:

https://android.googlesource.com/platform/packages/apps/Dialer/

对于Lollipop或Marshmallow,或N,它有SearchEditTextLayout,这是你需要的:

优势

  • 由Google制作的代码,而非第三方代码。

答案 1 :(得分:2)

您必须使用SearchView的自定义实现。

https://github.com/Quinny898/PersistentSearch

Android Studio:

如果您还没有添加Sonatype存储库:

maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }

将其作为依赖项导入:

compile 'com.quinny898.library.persistentsearch:library:1.1.0-SNAPSHOT'

在你的布局中:

<com.quinny898.library.persistentsearch.SearchBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchbox"
        />

输出:

enter image description here

答案 2 :(得分:0)

我自己正试图建立这个。结束只是使搜索按钮成为ImageButton。单击此按钮将触发要添加到顶部的片段,其中将包含布局内的Edittext,使其看起来像这样。此片段包含此搜索框下方的列表视图以显示建议。

提示:保留Fragment的rootlayout不带任何背景,并将clickable设置为true以使用透明窗格获得最佳效果