FragmentTransaction replace() 方法仅替换布局中的某些元素

FragmentTransaction replace() method only replacing some elements in layout

所以我有一个 activity 处理片段事务来替换不同的布局。我最初在方法 onCreated() 中替换了我的登录片段布局,然后当登录片段调用我的 activity 中的侦听器方法时,我尝试将其替换为我的地图片段布局。我的登录布局看起来不错,但是当我切换到地图布局时,一半的登录按钮和字段仍在屏幕上,与地图重叠 activity。我的容器上有洞吗?

Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_layo);
    fm = getFragmentManager();
    if (lf == null) {
        if (savedInstanceState != null) {
            lf = (LoginFragment) fm.findFragmentByTag("Login");
            return;
        }
    }
    FragmentTransaction ft = fm.beginTransaction();
    lf = new LoginFragment();
    ft.replace(R.id.mainContainer, lf);
    ft.addToBackStack(null);
    ft.commit();
}

@Override
public void onLogIn(String sessionID) {
    FragmentTransaction ft = fm.beginTransaction();
    MapScreenFragment mapFrag = new MapScreenFragment();
    ft.replace(R.id.loginpage, mapFrag);
    ft.addToBackStack(null);
    ft.commit();
}

fragment_login:

<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="login.shogun.comet.halp.LoginFragment"
android:orientation="vertical"
android:id="@+id/loginpage"
android:weightSum="1"
android:gravity="center"
android:background="@color/halp1Color">

<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:layout_marginTop="60dp"
    android:gravity="center"
    android:layout_below="@+id/HalpLogo" />

<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_below="@+id/username"
    android:layout_alignStart="@+id/username"
    android:layout_marginTop="10dp" />

LoginFragment 设置为:

inflater.inflate(R.layout.fragment_login, container, false);

fragment_map:

<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"
android:id="@+id/map">

<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/fragment_map" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fakeMarker"
    android:src="@drawable/student_turq"
    android:background="@color/transparent"
    android:contentDescription="@string/fakeMarker"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

MapScreenFragment 设置为:

inflater.inflate(R.layout.fragment_map, container, false);

为了简洁起见,我删掉了一堆按钮,而且我在 activity 中设置内容视图和更改为简洁起见也被删除的片段之间也有逻辑。

感谢您的宝贵时间!

您需要更换使用相同的容器。尝试将第二个替换更改为 R.id.mainContainer.

 FragmentTransaction ft = fm.beginTransaction();
    lf = new LoginFragment();
    ft.replace(R.id.mainContainer, lf); <-----
    ft.addToBackStack(null);
    ft.commit();
}
@Override
public void onLogIn(String sessionID) {
    FragmentTransaction ft = fm.beginTransaction();
    MapScreenFragment mapFrag = new MapScreenFragment();
    ft.replace(R.id.loginpage, mapFrag); <------ change to R.id.mainContainer
    ft.addToBackStack(null);
    ft.commit();
}

另一个简单的解决方案是将 fragment_map 的主要 RelativeLayout 的背景颜色设置为白色。