您的位置:首页 > 其它

Fragment

2016-04-01 14:32 176 查看
<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:background="#ff0"

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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: