銆怉ndroid 寮€鍙戞暀绋嬨€戝姩鎬佹坊鍔燜ragments
16lz
2021-01-25
鏈珷鑺傜炕璇戣嚜銆夿eginning-Android-4-Application-Development銆嬶紝濡傛湁缈昏瘧涓嶅綋鐨勫湴鏂癸紝鏁鎸囧嚭銆?/em>
鍘熶功璐拱鍦板潃http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/fragment鐨勭湡姝g敤澶勬槸鍦ㄧ▼搴忚繍琛岃繃绋嬩腑鍔ㄦ€佸湴娣诲姞銆?br>
1. 鏂板缓宸ョ▼銆?/p>
2. res/layout/main.xml
[java] view plain copy- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00FF00"
- android:orientation="vertical">
- <TextView
- android:id="@+id/lblFragment1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Thisisfragment#1"
- android:textColor="#000000"
- android:textSize="25sp"/>
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFE00"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Thisisfragment#2"
- android:textColor="#000000"
- android:textSize="25sp"/>
- </LinearLayout>
- publicclassFragment1extendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
- BundlesavedInstanceState){
- //---Inflatethelayoutforthisfragment---
- returninflater.inflate(R.layout.fragment1,container,false);
- }
- }
- publicclassFragment2extendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
- BundlesavedInstanceState){
- //---Inflatethelayoutforthisfragment---
- returninflater.inflate(R.layout.fragment2,container,false);
- }
- }
7. FragmentsActivity.java
[java] view plain copy
- publicclassFragmentsActivityextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- FragmentManagerfragmentManager=getFragmentManager();
- FragmentTransactionfragmentTransaction=fragmentManager
- .beginTransaction();
- //---getthecurrentdisplayinfo---
- WindowManagerwm=getWindowManager();
- Displayd=wm.getDefaultDisplay();
- if(d.getWidth()>d.getHeight()){
- //---landscapemode---
- Fragment1fragment1=newFragment1();
- //android.R.id.contentreferstothecontent
- //viewoftheactivity
- fragmentTransaction.replace(android.R.id.content,fragment1);
- }else{
- //---portraitmode---
- Fragment2fragment2=newFragment2();
- fragmentTransaction.replace(android.R.id.content,fragment2);
- }
- //---addtothebackstack---
- fragmentTransaction.addToBackStack(null);
- fragmentTransaction.commit();
- }
- }
8. 璋冭瘯銆?/p>
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer