③对应每个片段,你需要在XML文件中创建布局。这些文件中包含已定义的碎片的布局。
④最后,基于需求修改活动文件来定义实际的碎片替换逻辑。
2)在一个活动中添加两个碎片
①左侧碎片布局 left_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@android:color/holo_blue_light”>
<Button
android:id=“@+id/button”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“This is left fragment”
android:textAllCaps=“false”/>
②右侧碎片布局 right_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@android:color/holo_orange_light”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“This is right fragment”
android:textSize=“20sp” />
③新建一个LeftFragment类,继承Fragment
建议使用support-v4库的Fragment,如果在Fragment中嵌套Fragment,因为这个功能在Android4.2才开始支持,如果使用系统内置Fragment。4.2系统之前设备运行此程序就会崩溃
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.ledt_fragment, container, false);
return view;
}
}
④新建一个RightFragment类,继承Fragment
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
⑤修改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”
tools:context=“com.liang.fragmenttest.MainActivity”
android:orientation=“horizontal”>
<fragment
android:id=“@+id/left_fragment”
android:name=“com.liang.fragmenttest.LeftFragment”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”/>
<fragment
android:id=“@+id/right_fragment”
android:name=“com.liang.fragmenttest.RightFragment”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”/>
3)动态添加碎片
碎片真正强大之处在于,它可以在程序运行时动态地添加到活动中。
①在上一步的基础上,新建another_right_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@android:color/holo_purple”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:textSize=“20sp”
android:text=“This is another right fragment”/>
②新建AnotherRightFragment类
public class AnotherRightFragment extends Fragment {
@Nullable
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
尾声
开发是需要一定的基础的,我是08年开始进入Android这行的,在这期间经历了Android的鼎盛时期,和所谓的Android”凉了“。中间当然也有着,不可说的心酸,看着身边朋友,同事一个个转前端,换行业,其实当时我的心也有过犹豫,但是我还是坚持下来了,这次的疫情就是一个好的机会,大浪淘沙,优胜劣汰。再等等,说不定下一个黄金浪潮就被你等到了。
这是我在这行工作10几年积累的一些资料,如果还想继续在这行业走下去的,或者现在打算跳槽,可以**私信【学习】**我愿意把资料免费分享给大家。
或者直接点击下面链接领取
Android学习PDF+架构视频+面试文档+源码笔记
- 330页 PDF Android核心笔记
- 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题
- PDF和思维脑图,包含知识脉络 + 诸多细节
- Android进阶系统学习视频
- 330页 PDF Android核心笔记
[外链图片转存中…(img-jCz8UD2a-1710768176940)]
- 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题
[外链图片转存中…(img-O8qok02b-1710768176940)]
[外链图片转存中…(img-HwKFuQY2-1710768176940)]
- PDF和思维脑图,包含知识脉络 + 诸多细节
[外链图片转存中…(img-vcIXIHVj-1710768176941)]
- Android进阶系统学习视频