Fragment

来源:互联网 发布:hbase不直接删除数据 编辑:程序博客网 时间:2024/05/01 15:11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >
    <RelativeLayout
        android:layout_weight="1"
        android:id="@+id/rl_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>
    <RelativeLayout
        android:layout_weight="1"
        android:id="@+id/rl_2"
               android:background="#f00"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>

</LinearLayout>

--------------------------------------------------------------------------------------------------------------------------------------

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 得到Fragment的管理者
        FragmentManager supportFragmentManager = getSupportFragmentManager();

        // 开启事务
        FragmentTransaction beginTransaction = supportFragmentManager
                .beginTransaction();
        // 得到Fragment_1对象
        Fragment_1 fragment_1 = new Fragment_1();
        Fragment_2 fragment_2 = new Fragment_2();

        // 添加Fragment_1到布局文件;参数一:被填充的布局,参数二:要填充的fragment
        beginTransaction.add(R.id.rl_1, fragment_1);
        
        beginTransaction.add(R.id.rl_2, fragment_2);
        
        // 提交事务
        beginTransaction.commit();
    }

}
1 0