【问题标题】:Adding a border to a Google PlaceAutocompleteFragment向 Google PlaceAutocompleteFragment 添加边框
【发布时间】:2017-03-28 08:21:34
【问题描述】:

我在 Android 应用中使用 Google 地方信息自动完成片段。当我直接在 LinearLayout 的顶层使用片段时,一切正常:

<LinearLayout ...>
    <fragment
        android:id="@+id/findRidePlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"/>
</LinearLayout>

这个布局导致这个 UI:

如您所见,小部件周围没有边框,而它下方的 TextView 有黑色边框。根据我的阅读,在片段周围设置边框的一个技巧是将该片段嵌入到 CardView 中。这个方法其实是谷歌的official documentation推荐的。考虑以下修改后的布局:

<LinearLayout ...>
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/main_border">

        <fragment
            android:id="@+id/findRidePlaceAutocompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"/>
    </android.support.v7.widget.CardView>
</LinearLayout>

这里的技巧是给 CardView 一个背景,上面有 Google Places 片段,这样前者会显示为后者的边框。

但是这种修改后的布局会导致应用崩溃。有谁知道为什么会崩溃,或者我如何在 Google PlaceAutocompleteFragment 周围设置边框?

【问题讨论】:

  • 你能发布 logcat 错误吗?
  • 在哪里可以找到错误输出?我是 Android 开发新手。
  • 您可以在 Android Studio 的 Android Monitor 选项卡下找到它,也可以在 logcat 上找到它 选项卡,或者您可以在命令行或终端上键入adb logcat

标签: android google-maps android-fragments autocomplete


【解决方案1】:

您可以为封闭的LinearLayout设置自定义背景:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/main_border">
    <fragment
        android:id="@+id/findRidePlaceAutocompleteFragment"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

我正在使用您为CardView 定义的相同android:background="@drawable/main_border"。在我的情况下,main_border.xml(位于 res/drawable 文件夹中)如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#000000"/>
</shape>

【讨论】:

  • 感谢您的回复。目前我的LinearLayout 中有其他小部件。小屋我还是会试试这个并回复你。
  • 你试过了吗?这个想法不是将背景添加到现有的LinearLayout,而是将CardView 替换为带有背景的LinearLayout
  • 是的,我建议将您的CardView 替换为LinearLayout
  • 嗨安东尼奥,你的解决方案运行良好,至少到目前为止它运行良好。即使在另一个 LinearLayout 中嵌套 LinearLayout 存在限制,我也看不出有任何问题,无论如何,您的回答至少对一些努力在这个 Google 小部件周围设置边框的人来说应该是有用的。
  • 我认为 Google 应该更新他们的文档。
猜你喜欢
  • 2022-12-01
  • 2015-12-29
  • 1970-01-01
  • 2023-03-12
  • 2011-04-06
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多