2014-09-21 139 views
0

我是非常新的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> 

我是使用上述幀佈局容器更換爲改變片段

內容

我的代碼如下:

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(); 

}

和我對SplashLoginFragment 一個新的XML有人能幫助我走出

+1

你不應該有framelayout中的圖像視圖和文本視圖。框架佈局是爲了容納一個組件。如果您確實需要使用可視化切換或飛濺片段,則可以使用它。 – Spidy 2014-09-21 06:32:35

回答

0

replace呼叫FragmentTransaction僅涉及Fragments。它不會觸及正在添加Fragment的容器中的任何其他視圖。您有2個選項:

  1. 創建一個單獨的ViewGroup來保存SplashLoginFragment
  2. 在提交交易前,從您的container_main中刪除所有視圖。
+0

以及我如何做第一個? – 2014-09-22 16:36:14

+0

什麼是第一個?你什麼意思? – 2014-09-22 16:38:14