Android 向Google PlaceAutocompleteFragment添加边框

Android 向Google PlaceAutocompleteFragment添加边框,android,google-maps,android-fragments,autocomplete,Android,Google Maps,Android Fragments,Autocomplete,我正在Android应用程序中使用Google Places自动完成片段。当我在LinearLayout的顶层直接使用片段时,一切正常: <LinearLayout ...> <fragment android:id="@+id/findRidePlaceAutocompleteFragment" android:layout_width="match_parent" android:layout_height="wrap

我正在Android应用程序中使用Google Places自动完成片段。当我在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中。这一方法实际上是谷歌公司推荐的。考虑以下修改的布局:

<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
周围放置边框吗?

您可以为封闭的
线性布局设置自定义背景:

<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>

您可以为封闭的
线性布局设置自定义背景:

<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>

您可以发布logcat错误吗?我在哪里可以找到错误输出?我是Android开发新手。你可以在Android Studio的Android监视器选项卡下找到它,也可以在logcat选项卡上找到它,或者你可以在命令行或终端上键入
adb logcat
。你可以发布logcat错误吗?我在哪里找到错误输出?我是Android开发新手。你可以在Android Studio上的Android监视器选项卡下找到它,也可以在logcat选项卡上找到它,或者你可以在命令行或终端上键入
adb logcat
,谢谢回复。目前我的
LinearLayout
中还有其他小部件。但我还是会试试这个,然后再打给你。你试过了吗?我们的想法不是将背景添加到现有的
LinearLayout
,而是将
cardwiew
替换为
LinearLayout
。是的,我建议将
cardwiew
替换为
LinearLayout
。嗨,安东尼奥,您的解决方案工作得很好,至少到目前为止它工作得很出色。即使将一个LinearLayout嵌套在另一个LinearLayout中有限制,我也看不出有任何问题,而且在任何情况下,您的答案都应该对至少一些正在努力为这个Google小部件设置边界的人有用。我认为Google应该更新他们的文档。感谢您的回复。目前我的
LinearLayout
中还有其他小部件。但我还是会试试这个,然后再打给你。你试过了吗?我们的想法不是将背景添加到现有的
LinearLayout
,而是将
cardwiew
替换为
LinearLayout
。是的,我建议将
cardwiew
替换为
LinearLayout
。嗨,安东尼奥,您的解决方案工作得很好,至少到目前为止它工作得很出色。即使将一个LinearLayout嵌套在另一个LinearLayout中有限制,我也看不到任何问题,而且在任何情况下,您的答案都应该对至少一些正在努力为这个Google小部件设置边界的人有用。我认为Google应该更新他们的文档。