前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1-AVI--Fragment基础使用

1-AVI--Fragment基础使用

作者头像
张风捷特烈
发布2018-09-26 16:27:25
8090
发布2018-09-26 16:27:25
举报
文章被收录于专栏:Android知识点总结
零、前言

[1].Fragment静态使用 [2].Fragment动态使用

一、Fragment静态使用

静态fragment.jpg

1.蓝色布局:blue.xml
代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:orientation="vertical" >
    <TextView
        android:id="@+id/blue_tv"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="蓝色Fragment"
        />
</RelativeLayout>
2.黄色布局:yellow.xml
代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
                android:background="@color/yellow"
              android:orientation="vertical" >
    <TextView
        android:id="@+id/yellow_tv"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="黄色Fragment"
        />
</RelativeLayout>
3.黄色Fragment:YellowFragment.java
代码语言:javascript
复制
/**
 * 作者:张风捷特烈<br/>
 * 时间:2018/8/28 0028:13:07<br/>
 * 邮箱:1981462002@qq.com<br/>
 * 说明:黄色Fragment
 */
public class YellowFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.yellow, container, false);
    }
}
4.绿色Fragment:BlueFragment.java
代码语言:javascript
复制
/**
 * 作者:张风捷特烈<br/>
 * 时间:2018/8/28 0028:13:07<br/>
 * 邮箱:1981462002@qq.com<br/>
 * 说明:绿色Fragment
 */
public class BlueFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        
        return inflater.inflate(R.layout.fragment_bottom, container, false);
    }
}
5.主布局:layout/activity_static.xml
代码语言:javascript
复制
<?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="vertical"
    tools:context=".activity.ActFragmentActivity">

    <fragment
        android:id="@+id/yellow_fragment1"

        android:name="com.toly1994.avi_fragment.staticFg.YellowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <View
        android:background="@color/gray_8f"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

    <fragment
        android:id="@+id/yellow_fragment2"

        android:name="com.toly1994.avi_fragment.staticFg.YellowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/blue_fragment"
        android:name="com.toly1994.avi_fragment.staticFg.BlueFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"/>
</LinearLayout>
6.使用:StaticFragmentActivity.java
代码语言:javascript
复制
public class StaticFragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static);
    }
}

二、动态使用
代码语言:javascript
复制
public class ActFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acti);
        initFragment();
    }

    /**
     * 动态加载Fragment
     */
    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象)
        ft.add(R.id.fl_title, new YellowFragment());
        ft.add(R.id.fl_content, new BlueFragment());
        ft.commit();//4.提交事务
    }
}

动态使用fragment.png

本文由张风捷特烈原创,转载请注明 更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94 张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com 你的喜欢与支持将是我最大的动力

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.08.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 零、前言
  • 一、Fragment静态使用
    • 1.蓝色布局:blue.xml
      • 2.黄色布局:yellow.xml
        • 3.黄色Fragment:YellowFragment.java
          • 4.绿色Fragment:BlueFragment.java
            • 5.主布局:layout/activity_static.xml
              • 6.使用:StaticFragmentActivity.java
              • 二、动态使用
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档