【发布时间】:2016-07-03 20:32:15
【问题描述】:
经过一番努力,我设法在我的 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: 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 直接附加到 map_fragment_container,我什至可以在我的智能手机上看到它,但它会有一个不可见的背景。
有什么提示吗?
【问题讨论】:
标签: android android-fragments google-places-api android-support-library supportmapfragment