Fragment
来源:互联网 发布:oracle导出整个数据库 编辑:程序博客网 时间:2024/06/11 00:45
###布局
<RelativeLayout 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" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button1"
android:text="大陆明星" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button2"
android:text="欧美明星" />
</LinearLayout>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ll" >
</FrameLayout>
</RelativeLayout>
###自定义fragment1 fragment2
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大陆明星"
/>
###自定义fragment2
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欧美明星"
/>
###MainActivity
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到FragmentManager fargment管理者对象
fragmentManager = getSupportFragmentManager();
// 开启事务,得到事务对象
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
//得到fragment对象
fragment1 = new Fragment1();
fragment2 = new Fragment2();
// 替换fragment
// beginTransaction.replace(R.id.fl, new Fragment1());
// 同时添加两个fragment
beginTransaction.add(R.id.fl,fragment1).add(R.id.fl,fragment2);
// 提交事务
beginTransaction.commit();
}
public void button1(View v) {
// 开启事务,得到事务对象
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
// 替换fragment
// beginTransaction.replace(R.id.fl, new Fragment1());
//点击大陆明星时,隐藏欧美明星,展示出大陆明星
beginTransaction.hide(fragment2).show(fragment1);
// 提交事务
beginTransaction.commit();
}
public void button2(View v) {
// 开启事务,得到事务对象
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
// 替换fragment
// beginTransaction.replace(R.id.fl, new Fragment2());
//点击欧美明星时,展示欧美明星,隐藏出大陆明星
beginTransaction.hide(fragment1).show(fragment2);
// 提交事务
beginTransaction.commit();
}
}
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- fragment
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- 小甲鱼PE详解之区块表(节表)和区块(节)续(PE详解05)
- linux下安装谷歌拼音输入法
- 使用Iframe标签session失效异常
- 不要让过去束缚你前进的脚步
- 字符串编码格式相互转换Unicode --UTF-8 -- GBK
- Fragment
- Bitmap——读取本地路径转换成bitmap的方法,以及避免outmemory
- 国内著名IT公司(百度、搜狗、网易、新浪)2012校园招聘笔试、面试小结
- 在生产环境中遇到的案例来推测Linux文件系统保护机制
- C++之类的声明、定义、存储
- 局部变量和全局变量
- 关于this的解析
- BToj 1030 硬币问题 【DP-完全背包】
- muduo : TcpConnection's Read Buffer