Java Android GMaps v2处理方向更改
我在使用谷歌地图片段处理方向变化时遇到了麻烦。 问题是,当我使用这些线来防止在方向改变时重新加载地图时,我没有加载我的土地布局,当我改变方向时,它会抛出 二进制XML文件行#19:膨胀类片段时出错 这是清单代码,我发现它可以防止地图重新加载。如果我忽略这些行,我的应用程序将处理方向更改,但每次更改方向时,地图都将重新加载Java Android GMaps v2处理方向更改,java,android,google-maps,android-fragments,Java,Android,Google Maps,Android Fragments,我在使用谷歌地图片段处理方向变化时遇到了麻烦。 问题是,当我使用这些线来防止在方向改变时重新加载地图时,我没有加载我的土地布局,当我改变方向时,它会抛出 二进制XML文件行#19:膨胀类片段时出错 这是清单代码,我发现它可以防止地图重新加载。如果我忽略这些行,我的应用程序将处理方向更改,但每次更改方向时,地图都将重新加载 android:configChanges="orientation|uiMode|screenSize|fontScale" android:screenOrientat
android:configChanges="orientation|uiMode|screenSize|fontScale"
android:screenOrientation="user"
这是我的片段
public class BlankFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_main_screen_map, container, false);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LayoutInflater inflater = LayoutInflater.from(getActivity());
populateViewForOrientation(inflater, (ViewGroup) getView());
}
private void populateViewForOrientation(LayoutInflater inflater, ViewGroup viewGroup) {
viewGroup.removeAllViewsInLayout();
View subview = inflater.inflate(R.layout.activity_main_screen_map, viewGroup);
}
}
这是我的活动
public class MainScreenMapActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainshit);
BlankFragment frag= new BlankFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container, frag)
.addToBackStack("myStack")
.commit();
}
}
我的端口布局
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33ffffff"
android:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#33ffffff"
android:elevation="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentTop="true" />
我的土地布局
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#33ffffff"
android:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_alignParentLeft="true">
<android.support.v7.widget.Toolbar
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:background="#33ffffff"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentTop="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
AFAIK,android会在您旋转设备时保存动态添加的片段及其状态。因此,您可以在活动的onCreate方法中检查先前添加的片段,或者不使用片段管理器。