2012-07-23 56 views
1

我有一個應用程序,當用戶單擊一個表,它會更改佈局。 下面是代碼如何添加布局之間的轉換(不是活動)

<TableRow 
       android:id="@+id/tableRow1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/cell_table" 
       android:onClick="goAir" > 

而爲的onClick方法:

public void goAir(View view){ 
    setContentView(R.layout.va_air); 
} 

如何添加布局變化之間的過渡。像幻燈片一樣。

謝謝

回答

2

有多種方法可以做到這一點。我更喜歡使用片段。

/** 
* Adding given Fragment into content area without transition. 
* This is good for initializing view. 
* @param f 
*/ 
private void addContentView(Fragment f){ 
    FragmentTransaction ft =getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.contentPane, f).commit(); 
} 

/** 
* Replacing Fragment in content area with given Fragment 
* @param f Fragment to display 
* @param tag String of the content area 
* @param animIn Resource ID for new screen transition. 
* @param animOut Resource ID for old screen transition. 
*/ 
private void replaceContentView(Fragment f, String tag, int animIn, int animOut){ 
    // -1 is passed when I want to use default animation. 
    if(animIn == -1) animIn = R.anim.fragment_slide_left_enter; 
    if(animOut == -1) animOut = R.anim.fragment_slide_left_exit; 
    FragmentTransaction ft =getSupportFragmentManager().beginTransaction(); 
    ft.setCustomAnimations(animIn, animOut); // Animate new view in and existing view out 

    ft.replace(R.id.contentPane, f, tag); // id of the FrameLayout to put the Fragment in 
    ft.commit(); 
} 
相關問題