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

Java FragmentTransaction replace()方法仅替换布局中的某些元素,java,android,android-layout,android-fragments,android-5.0-lollipop,Java,Android,Android Layout,Android Fragments,Android 5.0 Lollipop,因此,我有一个处理片段转换的活动来替换不同的布局。我最初在创建的方法中替换了我的登录片段布局,然后当登录片段在我的活动中调用侦听器方法时,我尝试用我的映射片段布局替换它。我的登录布局看起来不错,但当我切换到“地图”布局时,屏幕上仍有一半的登录按钮和字段与“地图”活动重叠。我的容器里有洞吗 活动: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

因此,我有一个处理片段转换的活动来替换不同的布局。我最初在创建的方法中替换了我的登录片段布局,然后当登录片段在我的活动中调用侦听器方法时,我尝试用我的映射片段布局替换它。我的登录布局看起来不错,但当我切换到“地图”布局时,屏幕上仍有一半的登录按钮和字段与“地图”活动重叠。我的容器里有洞吗

活动:

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();
}
片段\u登录:

<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" />
碎片图:

<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>
为了简洁起见,我剪掉了一堆按钮,在活动中设置内容视图和更改片段之间也有逻辑,而片段也是为了简洁而删除的


谢谢你的时间

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


另一个简单的解决方案是将碎片地图的主要相对物的背景色设置为白色。

所以第二个替换应该是replacelf,mapFrag?因为第一个参数必须是容器id。应该像ft.replaceR.id.mainContainer、mapFrag一样,这就像说要用mapFrag替换“mainContainer”的内容。
<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>
inflater.inflate(R.layout.fragment_map, container, false);
 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();
}