首页 > 学院 > 开发设计 > 正文

Fragment 的两种创建方式

2019-11-09 18:56:58
字体:
来源:转载
供稿:网友
一、fragment的静态创建步骤:(前提:写好自己的Fragment类,见上篇文章)在要用到fragment的Activity所对应的xml文件中添加fragment控件并为其添加name属性(android:name="包名.Fragment类名")和id属性(id不加的话会在程序运行时出现闪退)。代码如下:MainActivity.class
public class MainActivity extends AppCompatActivity {    @Override    PRotected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.zhiyuan3g.fragmenttest.MainActivity">    <fragment        android:name="com.zhiyuan3g.fragmenttest.Fragment1"        android:id="@+id/fragment1"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"/>    <fragment        android:name="com.zhiyuan3g.fragmenttest.Fragment2"        android:layout_width="0dp"        android:id="@+id/fragment2"        android:layout_height="match_parent"        android:layout_weight="1"/></LinearLayout>Fragment1.class(同Fragment2.class)
public class Fragment1 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View fragment1 = inflater.inflate(R.layout.fragment1, null);        return fragment1;    }}

运行效果:

二、Fragment的动态创建的步骤:(前提:写好自己的Fragment类,见上篇文章)1.创建Fragment的管理对象fragmentManager。FragmentManager fragmentManager = getFragmentManager();2.创建事务对象(Fragment事务对象不能抽取,因为每提交一次,就需要一个新的Fragment事务对象.(所有的事务都有这个特性))FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();3.动态创建Fragment

fragmentTransaction.replace(android.R.id.content, new Fragment1());

4.提交事务对象

fragmentTransaction.commit();

代码如下:MainActivity.class
/** * function:g根据横竖屏切换不同的Fragment显示。 */public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);        int height = windowManager.getDefaultDisplay().getHeight();        int width = windowManager.getDefaultDisplay().getWidth();        //Fragment的动态创建步骤:        //1.创建Fragment的管理对象fragmentManager        FragmentManager fragmentManager = getFragmentManager();        //2.创建事务对象        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        if (height > width) {            //3.动态创建Fragment            fragmentTransaction.replace(android.R.id.content, new Fragment1());        } else {            //3.动态创建Fragment            fragmentTransaction.replace(android.R.id.content, new Fragment2());        }        //4.提交事务对象        fragmentTransaction.commit();    }}activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.zhiyuan3g.fragmenttest.MainActivity">    </LinearLayout>Fragment1.class(同Fragment2.class)
public class Fragment1 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View fragment1 = inflater.inflate(R.layout.fragment1, null);        return fragment1;    }}fragment1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent"><TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="我是fragment1"    android:textSize="25dp"/></LinearLayout>

运行效果:

竖屏时显示

横屏时显示 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表