怎样制作一个网站,暖色系网站模板,绣花图案设计网站,微信小程序设计现在Fragment的应用真的是越来越广泛了#xff0c;之前Android在3.0版本加入Fragment的时候#xff0c;主要是为了解决Android Pad屏幕比较大#xff0c;空间不能充分利用的问题#xff0c;但现在即使只是在手机上#xff0c;也有很多的场景可以运用到Fragment了#xff…现在Fragment的应用真的是越来越广泛了之前Android在3.0版本加入Fragment的时候主要是为了解决Android Pad屏幕比较大空间不能充分利用的问题但现在即使只是在手机上也有很多的场景可以运用到Fragment了今天我们就来学习其中一个特别棒的应用技巧。 很多手机应用都会有一个非常类似的功能即屏幕的下方显示一行Tab标签选项点击不同的标签就可以切换到不同的界面如以下几个应用所示 上面三个应用从左到右分别是QQ、新浪微博和支付宝钱包可见这种底部标签式的布局策略真的非常常见。 那么话说回来这种效果到底是如何的呢熟悉Android的朋友一定都会知道很简单嘛使用TabHost就OK了但是殊不知TabHost并非是那么的简单它的可扩展性非常的差不能随意地定制Tab项显示的内容而且运行还要依赖于ActivityGroup。ActivityGroup原本主要是用于为每一个TabHost的子项管理一个单独的Activity但目前已经被废弃了。为什么呢当然就是因为Fragment的出现了查看Android官方文档中ActivityGroup的描述如下所示 可以看到在API 13的时候Android就已经将ActivityGroup废弃掉了并且官方推荐的替代方式就是使用Fragment因为它使用起来更加的灵活。那么剩下的问题就是如何借助Fragment来完成类似于TabHost一般的效果了因此我们自然要动起手来了。 在开始之前首先你必须已经了解Fragment的用法了如果你对Fragment还比较陌生的话建议先去阅读我前面的一篇文章 Android Fragment完全解析关于碎片你所需知道的一切 。 另外我们还应该准备好程序所需要的资源比如说每一个Tab项中所用到的图片。我已经事先从QQ里截好了几张图作为这个项目的资源稍后会连同源码一起给出。 新建一个项目起名就叫FragmentDemo这里我使用的是4.0的API。 下面开始编程工作这里我们首先需要去编写一个类似于QQ的主界面当然只会去编写界面最下方的TabHost部分而不会编写上面的内容界面部分因为内容界面是应该写在Fragment的布局里的。打开或新建activity_main.xml作为程序的主布局文件在里面加入如下代码 [html] view plaincopy LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical FrameLayout android:idid/content android:layout_widthmatch_parent android:layout_height0dp android:layout_weight1 /FrameLayout LinearLayout android:layout_widthmatch_parent android:layout_height60dp android:backgrounddrawable/tab_bg RelativeLayout android:idid/message_layout android:layout_width0dp android:layout_heightmatch_parent android:layout_weight1 LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_centerVerticaltrue android:orientationvertical ImageView android:idid/message_image android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/message_unselected / TextView android:idid/message_text android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:text消息 android:textColor#82858b / /LinearLayout /RelativeLayout RelativeLayout android:idid/contacts_layout android:layout_width0dp android:layout_heightmatch_parent android:layout_weight1 LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_centerVerticaltrue android:orientationvertical ImageView android:idid/contacts_image android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/contacts_unselected / TextView android:idid/contacts_text android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:text联系人 android:textColor#82858b / /LinearLayout /RelativeLayout RelativeLayout android:idid/news_layout android:layout_width0dp android:layout_heightmatch_parent android:layout_weight1 LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_centerVerticaltrue android:orientationvertical ImageView android:idid/news_image android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/news_unselected / TextView android:idid/news_text android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:text动态 android:textColor#82858b / /LinearLayout /RelativeLayout RelativeLayout android:idid/setting_layout android:layout_width0dp android:layout_heightmatch_parent android:layout_weight1 LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_centerVerticaltrue android:orientationvertical ImageView android:idid/setting_image android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/setting_unselected / TextView android:idid/setting_text android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:text设置 android:textColor#82858b / /LinearLayout /RelativeLayout /LinearLayout /LinearLayout 这段布局代码虽然有点长但其实主要就分为两部分。第一个部分就是FrameLayout这里只是给FrameLayout的id设置成content并没有在里面添加任何具体的内容因为具体的内容是要在后面动态进行添加的。第二个部分就是FrameLayout下面的LinearLayout这个LinearLayout中包含的就是整个类似于TabHost的布局。可以看到我们将这个LinearLayout又等分成了四份每一份中都会显示一个ImageView和一个TextView。ImageView用于显示当前Tab的图标TextView用于显示当前Tab的标题这个效果就会和QQ非常得类似。 既然是等分成了四分那接下来我们自然要去分别实现四个Fragment和它们的布局了。新建一个message_layout.xml作为消息界面的布局代码如下所示 [html] view plaincopy ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent LinearLayout android:layout_widthwrap_content android:layout_heightwrap_content android:layout_centerInParenttrue android:orientationvertical ImageView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/message_selected / TextView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:padding10dp android:text这是消息界面 android:textSize20sp / /LinearLayout /RelativeLayout 这个布局就相对简单多了只是在屏幕的正中央显示一个消息图标以及一段文字。 然后要去创建对应这个布局的Fragment。新建MessageFragment继承自Fragment代码如下所示 [java] view plaincopy public class MessageFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View messageLayout inflater.inflate(R.layout.message_layout, container, false); return messageLayout; } } 后面就是依葫芦画瓢把其它几个Fragment以及对应的布局创建出来。新建contacts_layout.xml作为联系人界面的布局代码如下所示 [html] view plaincopy ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent LinearLayout android:layout_widthwrap_content android:layout_heightwrap_content android:layout_centerInParenttrue android:orientationvertical ImageView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/contacts_selected / TextView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:padding10dp android:text这是联系人界面 android:textSize20sp / /LinearLayout /RelativeLayout 再新建ContactsFragment继承自Fragment代码如下所示 [java] view plaincopy public class ContactsFragment extends Fragment { Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View contactsLayout inflater.inflate(R.layout.contacts_layout, container, false); return contactsLayout; } } 然后新建news_layout.xml作为动态界面的布局代码如下所示 [html] view plaincopy ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent LinearLayout android:layout_widthwrap_content android:layout_heightwrap_content android:layout_centerInParenttrue android:orientationvertical ImageView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/news_selected / TextView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:padding10dp android:text这是动态界面 android:textSize20sp / /LinearLayout /RelativeLayout 再新建NewsFragment继承自Fragment代码如下所示 [java] view plaincopy public class NewsFragment extends Fragment { Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View newsLayout inflater.inflate(R.layout.news_layout, container, false); return newsLayout; } } 最后新建setting_layout.xml作为设置界面的布局代码如下所示 [html] view plaincopy ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent LinearLayout android:layout_widthwrap_content android:layout_heightwrap_content android:layout_centerInParenttrue android:orientationvertical ImageView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:srcdrawable/setting_selected / TextView android:layout_widthwrap_content android:layout_heightwrap_content android:layout_gravitycenter_horizontal android:padding10dp android:text这是设置界面 android:textSize20sp / /LinearLayout /RelativeLayout 再新建SettingFragment继承自Fragment代码如下所示 [java] view plaincopy public class SettingFragment extends Fragment { Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View settingLayout inflater.inflate(R.layout.setting_layout, container, false); return settingLayout; } } 这样我们就把每一个Fragment以及它们所对应的布局文件都创建好了。接下来也就是最关键的步骤了打开或新建MainActivity作为主Activity代码如下所示 [java] view plaincopy /** * 项目的主Activity所有的Fragment都嵌入在这里。 * * author guolin */ public class MainActivity extends Activity implements OnClickListener { /** * 用于展示消息的Fragment */ private MessageFragment messageFragment; /** * 用于展示联系人的Fragment */ private ContactsFragment contactsFragment; /** * 用于展示动态的Fragment */ private NewsFragment newsFragment; /** * 用于展示设置的Fragment */ private SettingFragment settingFragment; /** * 消息界面布局 */ private View messageLayout; /** * 联系人界面布局 */ private View contactsLayout; /** * 动态界面布局 */ private View newsLayout; /** * 设置界面布局 */ private View settingLayout; /** * 在Tab布局上显示消息图标的控件 */ private ImageView messageImage; /** * 在Tab布局上显示联系人图标的控件 */ private ImageView contactsImage; /** * 在Tab布局上显示动态图标的控件 */ private ImageView newsImage; /** * 在Tab布局上显示设置图标的控件 */ private ImageView settingImage; /** * 在Tab布局上显示消息标题的控件 */ private TextView messageText; /** * 在Tab布局上显示联系人标题的控件 */ private TextView contactsText; /** * 在Tab布局上显示动态标题的控件 */ private TextView newsText; /** * 在Tab布局上显示设置标题的控件 */ private TextView settingText; /** * 用于对Fragment进行管理 */ private FragmentManager fragmentManager; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 初始化布局元素 initViews(); fragmentManager getFragmentManager(); // 第一次启动时选中第0个tab setTabSelection(0); } /** * 在这里获取到每个需要用到的控件的实例并给它们设置好必要的点击事件。 */ private void initViews() { messageLayout findViewById(R.id.message_layout); contactsLayout findViewById(R.id.contacts_layout); newsLayout findViewById(R.id.news_layout); settingLayout findViewById(R.id.setting_layout); messageImage (ImageView) findViewById(R.id.message_image); contactsImage (ImageView) findViewById(R.id.contacts_image); newsImage (ImageView) findViewById(R.id.news_image); settingImage (ImageView) findViewById(R.id.setting_image); messageText (TextView) findViewById(R.id.message_text); contactsText (TextView) findViewById(R.id.contacts_text); newsText (TextView) findViewById(R.id.news_text); settingText (TextView) findViewById(R.id.setting_text); messageLayout.setOnClickListener(this); contactsLayout.setOnClickListener(this); newsLayout.setOnClickListener(this); settingLayout.setOnClickListener(this); } Override public void onClick(View v) { switch (v.getId()) { case R.id.message_layout: // 当点击了消息tab时选中第1个tab setTabSelection(0); break; case R.id.contacts_layout: // 当点击了联系人tab时选中第2个tab setTabSelection(1); break; case R.id.news_layout: // 当点击了动态tab时选中第3个tab setTabSelection(2); break; case R.id.setting_layout: // 当点击了设置tab时选中第4个tab setTabSelection(3); break; default: break; } } /** * 根据传入的index参数来设置选中的tab页。 * * param index * 每个tab页对应的下标。0表示消息1表示联系人2表示动态3表示设置。 */ private void setTabSelection(int index) { // 每次选中之前先清楚掉上次的选中状态 clearSelection(); // 开启一个Fragment事务 FragmentTransaction transaction fragmentManager.beginTransaction(); // 先隐藏掉所有的Fragment以防止有多个Fragment显示在界面上的情况 hideFragments(transaction); switch (index) { case 0: // 当点击了消息tab时改变控件的图片和文字颜色 messageImage.setImageResource(R.drawable.message_selected); messageText.setTextColor(Color.WHITE); if (messageFragment null) { // 如果MessageFragment为空则创建一个并添加到界面上 messageFragment new MessageFragment(); transaction.add(R.id.content, messageFragment); } else { // 如果MessageFragment不为空则直接将它显示出来 transaction.show(messageFragment); } break; case 1: // 当点击了联系人tab时改变控件的图片和文字颜色 contactsImage.setImageResource(R.drawable.contacts_selected); contactsText.setTextColor(Color.WHITE); if (contactsFragment null) { // 如果ContactsFragment为空则创建一个并添加到界面上 contactsFragment new ContactsFragment(); transaction.add(R.id.content, contactsFragment); } else { // 如果ContactsFragment不为空则直接将它显示出来 transaction.show(contactsFragment); } break; case 2: // 当点击了动态tab时改变控件的图片和文字颜色 newsImage.setImageResource(R.drawable.news_selected); newsText.setTextColor(Color.WHITE); if (newsFragment null) { // 如果NewsFragment为空则创建一个并添加到界面上 newsFragment new NewsFragment(); transaction.add(R.id.content, newsFragment); } else { // 如果NewsFragment不为空则直接将它显示出来 transaction.show(newsFragment); } break; case 3: default: // 当点击了设置tab时改变控件的图片和文字颜色 settingImage.setImageResource(R.drawable.setting_selected); settingText.setTextColor(Color.WHITE); if (settingFragment null) { // 如果SettingFragment为空则创建一个并添加到界面上 settingFragment new SettingFragment(); transaction.add(R.id.content, settingFragment); } else { // 如果SettingFragment不为空则直接将它显示出来 transaction.show(settingFragment); } break; } transaction.commit(); } /** * 清除掉所有的选中状态。 */ private void clearSelection() { messageImage.setImageResource(R.drawable.message_unselected); messageText.setTextColor(Color.parseColor(#82858b)); contactsImage.setImageResource(R.drawable.contacts_unselected); contactsText.setTextColor(Color.parseColor(#82858b)); newsImage.setImageResource(R.drawable.news_unselected); newsText.setTextColor(Color.parseColor(#82858b)); settingImage.setImageResource(R.drawable.setting_unselected); settingText.setTextColor(Color.parseColor(#82858b)); } /** * 将所有的Fragment都置为隐藏状态。 * * param transaction * 用于对Fragment执行操作的事务 */ private void hideFragments(FragmentTransaction transaction) { if (messageFragment ! null) { transaction.hide(messageFragment); } if (contactsFragment ! null) { transaction.hide(contactsFragment); } if (newsFragment ! null) { transaction.hide(newsFragment); } if (settingFragment ! null) { transaction.hide(settingFragment); } } } 这个类中的注释已经写得非常详细了下面我再带大家简单梳理一遍。在onCreate()方法中先是调用了initViews()来获取每个控件的实例并给相应的控件设置好点击事件然后调用setTabSelection()方法设置默认的选中项这里传入的0说明默认选中第1个Tab项。 那么setTabSelection()方法中又是如何处理的呢可以看到首先第一步是调用clearSelection()方法来清理掉之前的选中状态然后开启一个Fragment事务并隐藏掉所有的Fragment以防止有多个Fragment显示在界面上。接下来根据传入的index参数判断出选中的是哪一个Tab项并改变该Tab项的图标和文字颜色然后将相应的Fragment添加到界面上。这里注意一个细节我们添加Fragment的时候并没有使用replace()方法而是会先判断一下该Fragment是否为空如果是空的则调用add()方法添加一个进来如果不是空的则直接调用show()方法显示出来即可。那么为什么没有使用replace()方法呢这是因为replace()方法会将被替换掉的那个Fragment彻底地移除掉该Fragment的生命周期就结束了。当再次点击刚才那个Tab项的时候就会让该Fragment的生命周期重新开始onCreate()、onCreateView()等方法都会重新执行一遍。这显然不是我们想要的也和ActivityGroup的工作原理不符因此最好的解决方案就是使用hide()和show()方法来隐藏和显示Fragment这就不会让Fragment的生命周期重走一遍了。 设置完默认选中项后我们当然还可以通过点击Tab项来自由地切换界面这就会进入到onClick()方法中。onClick()方法中的逻辑判断非常简单当点击了消息标签时就会选中第1个tab项点击联系人标签时就会选中第2个tab项点击动态标签时就会选中第3个tab项点击设置标签时就会选中第4个tab项。都是通过调用setTabSelection()方法来完成的只是传入了不同的参数。 好了这样我们就将全部的代码都编写完成了下面就来运行一下吧。整个Tab的界面有点类似于QQ的感觉并且可以通过点击不同的Tab来切换界面如下图所示 另外这个Tab界面即使在横屏的情况下也有不错的适用性哦如下图所示 这样我们就成功使用Fragment编写出了和TabHost一样的效果。每个界面的具体逻辑就可以写在相应的Fragment里效果和之前写在Activity里是差不多的。如此一来我们终于可以和那个被废弃的ActivityGroup说再见了 好了今天的讲解到此结束有疑问的朋友请在下面留言。 源码下载请点击这里 原文http://blog.csdn.net/guolin_blog/article/details/13171191转载于:https://www.cnblogs.com/zhangs1986/p/3670405.html