1-AVI--Fragment基础使用
零、前言
[1].Fragment静态使用
[2].Fragment动态使用
一、Fragment静态使用
1.蓝色布局:blue.xml
<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
<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
/** * 作者:张风捷特烈<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
/** * 作者:张风捷特烈<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
<?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
public class StaticFragmentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static); } }
二、动态使用
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.提交事务 } }
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328
4.欢迎关注我的微信公众号,最新精彩文章,及时送达:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
react native ios打包 编译成功 Archive失败报错问题
报错问题如下: Invalid bitcode version (Producer: '902.0.39.1_0' Reader: '900.0.37_0')', using libLTO version 'LLVM version 9.0.0, (clang-900.0.37)' for architecture arm64 具体解决办法 image.png
- 下一篇
3-AVI--Activity与Fragment的数据传递
零、前言 [1].Activity向Fragment传数据 [2].Fragment向Activity传数据 [3].Fragment向Fragment传数据 一、Activity向Fragment传数据效果: ac2fg.gif 1.Ac2FgActivity.java public class Ac2FgActivity extends AppCompatActivity { @BindView(R.id.tv_data_content) EditText mTvDataContent; @BindView(R.id.btn_send_data) Button mBtnSendData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ac2fg); ButterKnife.bind(this); initFragment(); } private void initF...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Linux系统CentOS6、CentOS7手动修改IP地址
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Windows10,CentOS7,CentOS8安装Nodejs环境
- 设置Eclipse缩进为4个空格,增强代码规范