fragment的真正用处是在程序运行过程中动态地添加。
1. 新建工程。
2. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > </LinearLayout>
3. res/layout/fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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="This is fragment #1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
4. res/layout/fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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="This is fragment #2" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
5. Fragment1.java
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // ---Inflate the layout for this fragment--- return inflater.inflate(R.layout.fragment1, container, false); } }
6. Fragment2.java
public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // ---Inflate the layout for this fragment--- return inflater.inflate(R.layout.fragment2, container, false); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索安卓 fragment
, fragment 筛选
, fragment地图
, fragment跳转
, 动态添加view
, fragment+离线定位
, inflate
, viewgroup
, layout
, android fragment
, container
, 碎片fragment
, public
inflater
twincat3.1 入门手册、ejb 3.1从入门到精通、thinkphp3.1快速入门、thinkphp3.1快速入门4、docsis 3.1 标准简介,以便于您获取更多的相关知识。