Android中碎片的使用方法详细介绍

发布时间:2021-09-18 16:11:04 作者:chen
来源:亿速云 阅读:145

本篇内容主要讲解“Android中碎片的使用方法详细介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中碎片的使用方法详细介绍”吧!

Fragment的使用

其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用.碎片的使用分为两种,静态添加碎片和动态添加碎片,我们就先来看一下静态添加碎片如何实现.

静态添加碎片

首先,先建两个Layout文件,这就是碎片的布局文件,大家可能也发现了,Android Studio里面可以直接快速建立碎片,就像Activity一样,但是这样会生成很多没用的代码,所以我们还是选择自己创建碎片布局.

两个布局都很简单,里面只有一个居中显示的TextView,下面贴一下代码.

第一个布局文件:fragment_first.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="This is the first fragment!"/></LinearLayout>

第二个布局文件fragment_second.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/holo_green_light"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="This is the second fragment!"/></LinearLayout>

现在布局文件建完了,我们该建立他们对应的Fragment了,也就是后台代码了。新建两个类,分别叫FirstFragment和SecondFragment,都继承于Fragment,需要注意一点,我们教程里面所使用的Fragment全都是android.support.v4.app.Fragment这个包下的,这样更有利于程序的兼容性.

贴一下两个类的代码,也很简单,只是重写了onCreateView方法来加载不同的布局文件.

public class FirstFragment extends Fragment {private View view;//得到碎片对应的布局文件,方便后续使用//记住一定要重写onCreateView方法@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_first, container, false);//得到对应的布局文件return view;}}public class SecondFragment extends Fragment {private View view;//得到碎片对应的布局文件,方便后续使用//记住一定要重写onCreateView方法@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_second, container, false);//得到对应的布局文件return view;}}

好,基本的工作我们做完了,现在我们用两个Activity展示如何静态添加碎片和动态添加碎片.

静态添加控件的话,需要使用fragment控件,指定其名称是你刚才创建的Fragment就可以,让我们来看一下.

先贴一下第一个Activity的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="me.worldskills.android.testfragment.MainActivity"><fragmentandroid:id="@+id/main_firstfragment"android:name="me.worldskills.android.testfragment.FirstFragment"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></fragment><fragmentandroid:id="@+id/main_secondfragment"android:name="me.worldskills.android.testfragment.SecondFragment"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></fragment><Buttonandroid:id="@+id/main_btnGoNext"android:layout_width="match_parent"android:layout_height="50dp"android:text="Go to next page"android:textAllCaps="false"/></LinearLayout>

其中那个按钮是用来跳转到下一个界面的,也就是动态添加碎片案例的Activity,在这里可以忽略.

这里我们看见了,两个fragment分别指定name为FirstFragment和SecondFragment,也就是你刚才创建的两个Fragment,一定要记得加上包名.对了,还有一个问题,就是这样的话是没有预览的,如果想要预览,需要在fragment标签中加上一句代码:

Tools:layout="@layout/布局文件名称" .

好了,静态添加碎片就完成了,什么?就这么简单,对啊...就这么简单.

动态添加碎片

动态添加碎片我们就不需要用fragment控件了,而是需要用个FrameLayout控件,这是为什么呢,首先我们都知道FrameLayout中的控件,都是从左上角开始显示,不用进行位置控制,动态添加碎片其实就是向容器里面动态添加碎片,而fragment控件只能用来静态绑定一个碎片.

先贴一下第二个Activity的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_second"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/second_btnLoadFirst"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Load First!"android:textAllCaps="false"/><Buttonandroid:id="@+id/second_btnLoadSecond"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Load Second!"android:textAllCaps="false"/><FrameLayoutandroid:id="@+id/second_fl"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout></LinearLayout>

上面的两个按钮用来加载不同的碎片,而下面的FrameLayout就是碎片显示的容器.

废话不多说,贴代码:

import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;public class SecondActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//点击第一个按钮的时候加载第一个碎片findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.second_fl, new FirstFragment());transaction.commit();}});//点击第二个按钮的时候加载第二个碎片findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//简写}});}}

getSupportFragmentManager方法用来获得一个碎片管理器对象(使用这个方法的时候注意是android.support.v4.app包下的哦),然后通过这个方法开始一个碎片事物对象,这个对象比较关键,可以用来动态添加碎片,调用它的replace方法,会把指定容器里面的其他控件全部清除掉,然后添加新的碎片进去.在这里就是先把R.id.second_f1里面的控件清空,然后添加传入一个FirstFragment进去.

替换完之后一定要记得调用commit方法提交,要不然你的所有操作都不会生效,切记.

到此,相信大家对“Android中碎片的使用方法详细介绍”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Kubernetes中的日志详细介绍
  2. android中 碎片的生命周期

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:ceph集群的搭建方法

下一篇:ASP.NET Core中IdentityServer4如何实现Token令牌身份认证

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》