【问题标题】:Place Autocomplete in MapFragment: invisible on old APIs在 MapFragment 中放置自动完成功能:在旧 API 上不可见
【发布时间】:2016-07-03 20:32:15
【问题描述】:

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

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

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

我的ma​​p_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();
    }

}

我发现如果我只是将 Place Autocomplete 直接附加到 ma​​p_fragment_container,我什至可以在我的智能手机上看到它,但它会有一个不可见的背景。

有什么提示吗?

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多