将自动完成放在 MapFragment 中:在旧 API 上不可见

Place Autocomplete in MapFragment: invisible on old APIs

经过相当多的努力,我设法在我的 MapFragment 中放置了一个 Place Autocomplete。还是有问题。

在我的 Android 模拟器 (API 24) 上,它是可见的,我可以使用它:

在我的智能手机上 (API 19) 我看不到它,在地图加载之前有一小段时间。

我的map_layout.xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="top"
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        card_view:cardCornerRadius="4dp">
    </android.support.v7.widget.CardView>

</RelativeLayout>

MapFragment.java里面的代码是这个:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Initialise a new fragment inside of this one.
    mFragmentManager = getChildFragmentManager();
    mSupportMapFragment = (SupportMapFragment) mFragmentManager.findFragmentByTag("mapFragment");
    mSupportPlaceAutocompleteFragment = (SupportPlaceAutocompleteFragment) mFragmentManager.findFragmentByTag("autocompleteFragment");

    // Never inflate fragments inside other fragments in a layout.xml!
    // Do it programmatically.
    // See here for reference: 
    if (mSupportMapFragment == null) {
        mSupportMapFragment = new SupportMapFragment();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.add(R.id.map_fragment_container, mSupportMapFragment, "mapFragment");
        mFragmentTransaction.commit();
        mFragmentManager.executePendingTransactions();
    }

    // Asynchronous thread to load map.
    mSupportMapFragment.getMapAsync(this);

    if (mSupportPlaceAutocompleteFragment == null) {
        mSupportPlaceAutocompleteFragment = new SupportPlaceAutocompleteFragment();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.add(R.id.card_view, mSupportPlaceAutocompleteFragment, "autocompleteFragment");
        mFragmentTransaction.commit();
        mFragmentManager.executePendingTransactions();
    }

}

我发现如果我直接将地点自动完成附加到 map_fragment_container,我什至可以在我的智能手机上看到它,但它会有一个看不见的背景。

有什么提示吗?

只需将两个组件放在父组件中即可 FrameLayout:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/map_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="top"
        android:layout_margin="@dimen/margin_1"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_view_height"
        card_view:cardCornerRadius="@dimen/card_corner_radius"/>

</FrameLayout>