Java 使用片段事务的重叠布局
所以我尝试从我的主要活动中加载我的登录片段。我想当我使用片段转换替换方法时,在中没有替换整个布局是有问题的 以下是我的活动:Java 使用片段事务的重叠布局,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,所以我尝试从我的主要活动中加载我的登录片段。我想当我使用片段转换替换方法时,在中没有替换整个布局是有问题的 以下是我的活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); } @Override protected void onStart() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
@Override
protected void onStart() {
super.onStart();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LoginFragment lf = new LoginFragment();
ft.replace(R.id.mapView, lf);
ft.addToBackStack("map to login");
ft.commit();
}
activity_maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:showIn="@layout/activity_maps">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/activity_maps" />
<Button
android:id="@+id/btn
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
登录\u fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".LoginActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPersonName"
android:text="@string/Username"
android:ems="10"
android:id="@+id/username"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPersonName"
android:text="@string/Password"
android:ems="10"
android:id="@+id/password"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/ForgotPassword"
android:id="@+id/forgotPassword"
android:layout_gravity="center_horizontal"
android:textColor="#197cff" />
<Button
android:id="@+id/GoogleButton"
android:layout_width="215dp"
android:layout_height="wrap_content" />
</LinearLayout>
关于如何重构xml文件有什么建议吗
谢谢你的时间
关于如何重构xml文件有什么建议吗
您的问题与xml布局无关。首先,在活动布局中嵌入该MapFragment,然后用另一个片段替换它,因为活动startsonStart毫无意义。其次,嵌入的片段在xml中声明的带有片段标记的片段不能用于片段事务,它们是静态的。因此,请从xml布局中删除MapFragment,并执行一个简单的事务,将LoginFragment添加到活动布局中的空视图组中。是否应将所有onStart代码上移到onCreate,然后用新的视图组替换R.id.mapView,然后从login切换到map,我将删除静态片段,然后再从login\u片段转换到activity\u映射。如果这样做,我会将setContentView设置为什么?或者我应该将其设置为零?对不起,我还在想这是怎么回事!感谢您的快速回复@EmmettHarper我是否应该将所有onStart代码上移到onCreate-是。不清楚你在用那个地图碎片做什么。这里是添加片段的更改。地图片段是我活动的主要部分。我只想检查用户是否已登录,如果他们在看到地图之前未登录,则将他们发送到该片段,否则我将仅将用户切换到地图片段。从地图片段中,我计划切换到其他几个片段fragment@EmmettHarper这更有意义。您需要在activity_maps.xml中将MapFragment代码添加到containera空FrameLayout或空RelativeLayout等中,然后在另一个事务中添加LoginFragment(如有必要)。选中此项@EmmettHarper您的活动不应该扩展MapActivity如果这是您现在正在做的,因为它已被弃用,它应该扩展普通活动类。与地图相关的代码应该位于扩展MapFragment的片段中。活动应该充当这些片段的容器。