Android 从主活动调用片段会添加片段

Android 从主活动调用片段会添加片段,android,android-fragments,replace,Android,Android Fragments,Replace,我对android非常陌生,我一直在从主要活动中调用片段。问题是,每次我说用新的布局替换旧的framelayout时,它只会添加到原始布局,而不会替换视图 我的activity_main.xml如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/splas

我对android非常陌生,我一直在从主要活动中调用片段。问题是,每次我说用新的布局替换旧的framelayout时,它只会添加到原始布局,而不会替换视图

我的activity_main.xml如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/splash_screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jaggz.gurudawaraapp.MainActivity" >

<FrameLayout
    android:id="@+id/container_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="126dp"
        android:layout_marginTop="29dp"
        android:src="@drawable/splash" />

    <TextView
        android:id="@+id/splash_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="25dp"
        android:paddingLeft=" 30dp"
        android:paddingRight="10dp"
        android:paddingTop="300dp"
        android:text="@string/splash_text"
        android:textSize="@dimen/splash_text_size"
        android:textStyle="bold|italic"
        android:typeface="sans" />
</FrameLayout>
</RelativeLayout>
}

我为SplashLoginFragment提供了一个新的xml 有人能帮我吗?

在FragmentTransaction中的替换调用只涉及片段。它不会触及您正在添加片段的容器中的任何其他视图。您有两种选择:

创建单独的视图组以容纳SplashLoginFragment。 在提交事务之前,从容器_main中删除所有视图。
框架布局中不应该有图像视图和文本视图。框架布局旨在容纳单个构件。如果你真的需要一个启动ui视图,你可以通过可见性切换或启动片段来实现。我该怎么做第一个呢?第一个是什么?什么意思?
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.app = (App) getApplication().getApplicationContext();



    init();

}


private void init() {
     Fragment frag = new SplashLoginFragment();
     android.support.v4.app.FragmentManager fm1 = MainActivity.this.getSupportFragmentManager();
     FragmentTransaction ft1 = fm1.beginTransaction();
     ft1.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
     ft1.replace(R.id.container_main, frag);
     ft1.commit();