界面类似上图,点击下部控制栏按键,会对应响应不同的界面
本文参考郭神的文章进行应用,原文链接:http://blog.csdn.net/guolin_blog/article/details/13171191
1、内容界面是应该写在Fragment的布局里的。打开或新建activity_main.xml作为程序的主布局文件,在里面加入如下代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/login_bg" > <RelativeLayout android:id="@+id/message_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/message_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/first_page" /> <TextView android:id="@+id/message_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="首页" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/contacts_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/contacts_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/telephone" /> <TextView android:id="@+id/contacts_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="河长通讯录" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/news_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/news_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/notice" /> <TextView android:id="@+id/news_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="我的通知" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/setting_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/setting_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/person" /> <TextView android:id="@+id/setting_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_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非常得类似。
2、分别实现四个Fragment和它们的布局了。新建一个message_layout.xml作为消息界面的布局,代码如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" > <android.support.v7.widget.Toolbar android:id="@+id/grid_view_all_toolbar" android:layout_height="48sp" android:layout_width="match_parent" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:background="@color/colorPrimary"> <TextView android:id="@+id/grid_view_allApp_toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:textStyle="bold" android:layout_gravity="center" android:textSize="16sp" /> </android.support.v7.widget.Toolbar>
</RelativeLayout>
这个布局就相对简单多了,只是在屏幕最上方加了一个”首页“的标题,这里要注意的是需要把主题换成NoActionBar模式,模式代码如下:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> </style> </resources>
3、然后要去创建对应这个布局的Fragment。新建MessageFragment继承自Fragment,代码如下所示:
public class MessageFragment extends Fragment { private TextView inspect; private TextView record; private TextView message; private TextView data; private TextView photo; private TextView files; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View messageLayout=inflater.inflate(R.layout.message_layout,container,false); inspect= (TextView) messageLayout.findViewById(R.id.inspect); inspect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getActivity(),InspectActivity.class); startActivity(intent); } }); return messageLayout; } }
这里我还在fragment里面添加了一个点击事件,绑定的时候要注意前面加上messagelayout,还有跳转的时候Intent里面注意加上getActivity
4、后面就是依葫芦画瓢,把其它几个Fragment以及对应的布局创建出来。新建contacts_layout.xml作为联系人界面的布局
5、们就把每一个Fragment,以及它们所对应的布局文件都创建好了。接下来也就是最关键的步骤了,打开或新建MainActivity作为主Activity,代码如下所示:
public class MainActivity extends BaseActivity implements View.OnClickListener{ private TextView inspect; private TextView record; private TextView message; RadioButton address; private GridView mGridView; private String IMAGE_ITEM = "imgage_item"; private String TEXT_ITEM = "text_item"; /** * 用于展示首页的Fragment */ private MessageFragment messageFragment; /** * 用于展示联系人的Fragment */ private ContactsFragment contactsFragment; /** * 用于展示通知的Fragment */ private NoticeFragment newsFragment; /** * 用于个人设置的Fragment */ private PersonFragment settingFragment; /** * 首页界面布局 */ private View messageLayout; /** * 联系人界面布局 */ private View contactsLayout; /** * 通知界面布局 */ private View noticeLayout; /** * 个人设置界面布局 */ private View personLayout; /** * 在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 = getSupportFragmentManager(); // 第一次启动时选中第0个tab setTabSelection(0); } /** * 在这里获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件。 */ private void initViews() { messageLayout = findViewById(R.id.message_layout); contactsLayout = findViewById(R.id.contacts_layout); noticeLayout = findViewById(R.id.news_layout); personLayout = 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); noticeLayout.setOnClickListener(this); personLayout.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.first_page); messageText.setTextColor(Color.RED); 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.telephone); contactsText.setTextColor(Color.RED); 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.notice); newsText.setTextColor(Color.RED); if (newsFragment == null) { // 如果NewsFragment为空,则创建一个并添加到界面上 newsFragment = new NoticeFragment(); transaction.add(R.id.content, newsFragment); } else { // 如果NewsFragment不为空,则直接将它显示出来 transaction.show(newsFragment); } break; case 3: // 当点击了设置tab时,改变控件的图片和文字颜色 settingImage.setImageResource(R.drawable.person); settingText.setTextColor(Color.RED); if (settingFragment == null) { // 如果SettingFragment为空,则创建一个并添加到界面上 settingFragment = new PersonFragment(); transaction.add(R.id.content, settingFragment); } else { // 如果SettingFragment不为空,则直接将它显示出来 transaction.show(settingFragment); } break; default: break; } transaction.commit(); } /** * 清除掉所有的选中状态。 */ private void clearSelection() { messageImage.setImageResource(R.drawable.first_page); messageText.setTextColor(Color.parseColor("#82858b")); contactsImage.setImageResource(R.drawable.telephone); contactsText.setTextColor(Color.parseColor("#82858b")); newsImage.setImageResource(R.drawable.notice); newsText.setTextColor(Color.parseColor("#82858b")); settingImage.setImageResource(R.drawable.person); 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); } }}
6、在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的生命周期重走一遍了。