android - 如何像在 Google Play 商店应用中一样设置 Android SearchView 的样式?

标签 android google-play themes android-appcompat searchview

我正在尝试为 toolbar 中的 SearchView 设置样式,使其看起来/表现得像 Google Play Store 应用中的 SearchView 。它似乎将搜索 View 包装在卡片 View 中,但它似乎也集成了向上按钮/抽屉切换行为。

这是主要的 activity searchView,它集成了抽屉开关

MainActivity SearchView

当您点击它时,抽屉切换变为箭头(点击时将从搜索 View 中移除焦点)

enter image description here

当您在商店中点击一个应用程序时,您会转到应用程序详细信息页面,并且您会看到一个看起来像图标化版本的搜索 View ,作为一个带有向上按钮的折叠操作项:

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 替换它。这允许我控制填充,但显然我失去了工具栏功能(向上按钮等)并且还引入了主题问题(图标和文本消失)。

有人知道他们是怎么做到的吗?我不想添加另一个库来为框架中已经存在的小部件设置样式。谢谢!

最佳答案

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

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

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

优势

  • 由 Google 而不是第三方制作的代码。

关于android - 如何像在 Google Play 商店应用中一样设置 Android SearchView 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38327504/

相关文章:

java - 为什么我在 Android Media Player 中遇到这 3 个疯狂的错误

java - SQL(查询)错误或缺少数据库,即使在创建数据库和表后也是如此

android - NDK 一般段错误,decStrong?

java - 在哪里/如何在 ListView 适配器中取消注册广播接收器?

android - InApp帐单无效信用卡

android - androidx.activity.ComponentActivity 的父类(super class) androidx.core.app.f 被声明为 final

google-play - Galaxy S3 谷歌播放不工作

wpf - 如何在 wpf 中实现主题

android - 支持 Android Pie 中的浅色和深色主题

wpf - 如何在没有 App.xaml 的情况下制作全局可用的样式