2016-11-14 4 views
0

AndroidアプリでGoogleプレイスオートコンプリートを使用しています。私はのLinearLayoutのトップレベルで直接フラグメントを使用すると、すべてが動作します:Google PlaceAutocompleteFragmentにボーダーを追加する

<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につながる:

enter image description here

あなたが見ることができるように、周りに境界線がありませんその下のTextViewには黒い境界があります。私が読んだところから、フラグメントの周りに境界線をつけるトリックは、そのフラグメントをCardViewに埋め込むことです。この方法は、Googleの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プレイスフラグメントと背景を与えることです。

しかし、この変更されたレイアウトは、アプリケーションをクラッシュさせます。なぜこれがクラッシュしているのか、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> 

私はあなたがあなたのCardViewのために定義されている同じandroid:background="@drawable/main_border"を使用しています:

+1

あなたはlogcatエラーを投稿してもらえますか? – antonio

+0

エラー出力はどこにありますか?私はAndroid開発の初心者です。 –

+0

Android Studioの*** Android Monitorの***タブ、*** logcat ***タブ、またはコマンドラインまたは端末に 'adb logcat'と入力すると見つかります – antonio

答えて

2

あなたが囲むLinearLayoutのカスタム背景を設定することができます。私の場合は(res/drawableフォルダに住んでいる)main_border.xmlは次のようになります。

<?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> 
+0

ありがとう返信現在私の 'LinearLayout'には他のウィジェットがあります。ハット私はまだこれを試し、あなたに戻って取得します。 –

+0

これは試しましたか?このアイデアは、既存の 'LinearLayout'に背景を追加するのではなく' CardView'を 'LinearLayout'で背景を置き換えます。 – antonio

+0

このアクティビティのすべては既に' LinearLayout'内にあります。既に存在するものの中に 'LinearLayout'を置くことを提案していますか? –

関連する問題