Fragment初步学习
2015-12-02 11:52
295 查看
在布局里引用fragment
1、建两个类,继承自Fragmentpublic class MyFirstFragment extends Fragment { //实例化此类,在其他类中直接调用此方法,不用再去new这个类 public static Fragment newInstance(){ MyFirstFragment firstFragment = new MyFirstFragment(); return firstFragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_my_first_layout, container, false); }
2、每个类都有一个布局,我的每个布局里只放了个TextView
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/first_fragment" android:gravity="center"/>
3、建一个类继承自FragmentActivity
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_fragment_layout); } }
4、在Activity的布局里引用fragment,(注意:引用fragment时,一定要设置id,不然启动模拟器时引用布局会报错)
<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="horizontal"> <fragment android:id="@+id/firstfragmnet" android:name="com.example.fragment.fragmentapplication.MyFirstFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/secondfragment" android:name="com.example.fragment.fragmentapplication.MySecondFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>
在Activity类里动态添加fragment
1、先在Activity布局里用FrameLayout确定两个位置<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="horizontal"> <FrameLayout android:id="@+id/firstfragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/secondfragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>
2、在类里用代码引用fragment,(记住最后一定要调用commit()提交),getSupportFragmentManager()是v4包下的,后面引用的各种类也一定要是v4包下的;getFragmentManager()是本身自带的,后面引用的类也要是本身自带的,
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_fragment_layout); //分开的每一步 /* FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.firstfragment,MyFirstFragment.newInstance()); fragmentTransaction.add(R.id.secondfragment,MySecondFragment.newInstance()); fragmentTransaction.commit();*/ getFragmentManager().beginTransaction().add(R.id.firstfragment, MyFirstFragment.newInstance()).add(R.id.secondfragment, MySecondFragment.newInstance()).commit(); } }
相关文章推荐
- android 程序防止被360或者系统终止
- nginx nginx.conf配置说明
- 字典
- Eclipse快捷键
- 开启mysql事件
- C语言编程中实现二分查找的简单入门实例
- java编程思想(读书笔记):3.控制程序流程
- Android Studio Unable to execute DX
- Android 自定义的Dialog
- 同步与异步 阻塞与非阻塞
- [转] ServletContext 与application的异同
- 253. Meeting Rooms II
- PV VG LV 的简单使用
- java编程思想(读书笔记):2.万事万物皆对象
- Android笔记:获取url或uri字符串中的参数值
- android单元测试
- Android通过代码模拟物理、屏幕点击事件
- DAO设计模式之禅之数据库万能查询操作
- 安装nginx
- 1024. Palindromic Number (25)【简易大数加法+回文】——PAT (Advanced Level) Practise