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

时间:2016-07-03 20:32:16

标签: android android-fragments google-places-api android-support-library supportmapfragment

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

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

EMULATOR

在我的智能手机(API 19)上,除了在地图加载之前的一瞬间,我看不到它。

SMARTPHONE

我的 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: http://stackoverflow.com/a/19815266/4938112.
    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 ,我甚至可以在智能手机上看到它,但它会有一个看不见的背景。 / p>

任何提示?

1 个答案:

答案 0 :(得分:1)

只需将两个组件放在父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>