【Android】20、在运行时添加碎片
来源:互联网 发布:vb 按键精灵 编辑:程序博客网 时间:2024/05/01 17:24
1、接文章19中的案例,修改mainactivity.xml布局文件,删除或注释掉fragment标签
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--<fragment--> <!--android:name="com.wxy.fragment.Fragment1"--> <!--android:id="@+id/fragment1"--> <!--android:layout_width="0px"--> <!--android:layout_weight="1"--> <!--android:layout_height="match_parent"/>--> <!--<fragment--> <!--android:name="com.wxy.fragment.Fragment2"--> <!--android:id="@+id/fragment2"--> <!--android:layout_width="0px"--> <!--android:layout_weight="1"--> <!--android:layout_height="match_parent"/>--></LinearLayout>
2、修改mainactivity.java文件
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// setContentView(R.layout.main); android.app.FragmentManager fragmentManager = getFragmentManager(); android.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); //get the current display info WindowManager wm=getWindowManager(); Display d=wm.getDefaultDisplay(); if (d.getWidth()>d.getHeight()){ //landscape mode Fragment1 fragment1 =new Fragment1(); //android.R.id.content refers to the content //view of the activity fragmentTransaction.replace(android.R.id.content, fragment1); } else { //portrait mode Fragment2 fragment2 =new Fragment2(); fragmentTransaction.replace(android.R.id.content, fragment2); } fragmentTransaction.commit(); }}
0 0
- 【Android】20、在运行时添加碎片
- Android学习笔记(十四)——在运行时添加碎片(附源码)
- 【Android】51、动态添加碎片
- android学习(一) 在运行时为 Activity 添加Fragment
- Activity与在Android的碎片Fragment
- 如何在运行时添加classpath
- 在Activity运行时添加Fragment
- Android系统启动时添加运行脚本
- android之碎片(fragment)一(静态添加)
- android之碎片(fragment)二(动态添加)
- Android之Fragment入门二(动态添加碎片)
- 动态添加Fragment碎片
- 动态添加碎片
- 动态添加碎片
- Fragment动态添加碎片
- 动态添加碎片
- 静态添加碎片
- 【Android】52、在碎片中模拟返回栈
- 微服务化改造系列之一:总览
- SpringMVC - 非注解的处理器映射器和适配器
- LeetCode 126. Word Ladder II
- 有史以来最容易理解的控制反转(IoC)与注入依赖(DI)
- hdu1052-田忌赛马
- 【Android】20、在运行时添加碎片
- rar commandline
- 2010上海世博会各国展馆珍贵图片
- jQuery的基本选择器
- 常见信号的模拟仿真(matlab)(spike signal)
- 变量
- 密码学算法及应用(二)
- 学习笔记之openssl、文件加密、CA证书的创建
- Linux之编写运行第一个c语言程序