如何在布局之间添加转换(不是活动)

时间:2012-07-23 10:40:23

标签: android android-layout

我有一个应用程序,当用户单击一个tablerow时,它会更改布局。 这是代码

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

如何在布局更改之间添加转换。像幻灯片一样。

谢谢

1 个答案:

答案 0 :(得分:2)

有多种方法可以做到这一点。我更喜欢使用Fragment。

/**
 * 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();
}