android - 在 Android 应用程序中实现类似 Google map 的搜索栏

标签 android google-maps search

我正在构建一个使用 Google Maps API 的应用,我希望它在顶部有一个 float 搜索栏,就像 Google Maps 应用中的搜索栏一样。

我发现了地点自动完成功能,但该应用程序不会搜索地点,而是搜索用户创建的另一种数据。

我还发现了一个名为 Float Search View 的库,但它已经停用了一段时间,因此我想继续使用它。我还想手动创建它,因为这是我要构建的第一个 Android 应用程序,我想学习。

我已经尝试在我的 XML 中实现一个 SearchView:

<android.support.v7.widget.SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:iconifiedByDefault="false"
    app:queryHint="@string/search_hint"
    android:theme="@style/SearchBar"/>

白色背景和高度:

<style name="SearchBar">
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:elevation">4dp</item>
</style>

看看能不能接近它,结果很奇怪。

enter image description here

我试过其他方法,比如添加 Toolbar,但结果是一样的。

我想要一个类似于 Google map 顶部搜索栏的结果。我该怎么做?

最佳答案

Google map 用户界面可能没有什么特别之处。我的猜测是它是全屏 MapView 之上的自定义 View 。您可以通过在与全屏 MapView 相同的容器中使用类似工具栏的布局来做一些类似的事情。

这是一个让您入门的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_add_24dp" />

            <EditText
                android:hint="Try gas stations, ATMs"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_search_24dp" />

        </LinearLayout>

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

</android.support.constraint.ConstraintLayout>

这看起来像这样(想象一下红色是 MapView 所在的位置):

Android Studio Preview

从这里开始,您只需根据需要设置“工具栏”的样式。

希望对您有所帮助!

关于android - 在 Android 应用程序中实现类似 Google map 的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51120368/

相关文章:

android - 从 GPS 数据中找到最佳匹配路线

android - 从谷歌位置服务获取状态

Java 从具有多个条件的列表中搜索子列表

java - 减少格式化字符串所需的步骤

Android Parcelable 与 JsonObject

android - 如何在 View 失去焦点时屏蔽 EditText 中的文本。

android - 试图将 map 放入 fragment Activity 中

linux - 搜索多个目录并返回一个结果

MySQL 在一列的最后 5 个字符内搜索?

java - 关闭 NavigationDrawer 并在获取数据时显示加载