【Android UI设计与开发】第09期:Fragment+PopupWindow仿QQ空间最新版底部菜单栏

来源:互联网 发布:淘宝广告自己跳出来 编辑:程序博客网 时间:2024/05/30 22:58

首先申明,本系列【Android UI设计与开发】都为转载而来,为了方便自己学习查看,同时也方便大家共同学习前辈们积累的知识,特此转载本系列教程。本系列出处jingqing,空间链接为:http://www.apkbus.com/home.php?mod=space&uid=659&do=indexjingqing空间中还有更多经典的教程!在此,特别感谢jingqing。


在今天的这篇文章当中,我依然会以实战加理论结合的方式教大家如何设计出自己觉得很炫的UI界面。好的,话不多说,进入正题。今天的这篇文章主要是以仿QQ空间的底部菜单栏效果为主,实现的效果有:

<1>实现了点击按钮时的切换图片效果;

<2>实现了点击按钮时的切换界面效果;

<3>实现了点击中间圆形按钮时弹出菜单以及按钮图片切换效果;

<4>实现了点击空白处和返回键按钮来关闭弹出菜单。


          有个地方需要注意的是,弹出菜单栏后,点击里面的选项按钮会导致中间的圆形按钮切换为普通状态,这是因为在实际的项目中,点击菜单选项按钮之后会进入别的界面,所以也就不存在点击了之后圆形的按钮切换为普通的状态效果了,所以这里也不需要太在意。为了实现效果,这里只适配了480x800 hdpi的屏幕大小。


一、效果图


有图才有真相







二、项目结构图



三、代码详细编写


1、主布局界面,activity_main.xml:

  1. <span style="font-size:12px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" >

  5.     <FrameLayout
  6.         android:id="@+id/frame_content"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_above="@+id/frameLayout1"
  10.         android:layout_alignParentLeft="true"
  11.         android:layout_alignParentRight="true"
  12.         android:layout_alignParentTop="true"
  13.         android:background="#ffffff" >
  14.     </FrameLayout>

  15.     <FrameLayout
  16.         android:id="@+id/frameLayout1"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:layout_alignParentBottom="true"
  20.         android:layout_alignParentLeft="true"
  21.         android:background="@drawable/toolbar_bg_normal" >

  22.         <LinearLayout
  23.             android:layout_width="fill_parent"
  24.             android:layout_height="fill_parent"
  25.             android:layout_marginTop="1dp"
  26.             android:gravity="center_horizontal" >

  27.             <FrameLayout
  28.                 android:id="@+id/layout_friendfeed"
  29.                 android:layout_width="fill_parent"
  30.                 android:layout_height="48dp"
  31.                 android:layout_weight="1" 
  32.                 android:background="@drawable/tab_btn_background">

  33.                 <ImageView
  34.                     android:id="@+id/image_friendfeed"
  35.                     android:layout_width="wrap_content"
  36.                     android:layout_height="wrap_content"
  37.                     android:layout_gravity="top|center"
  38.                     android:layout_marginTop="1.0dip"
  39.                     android:src="@drawable/tab_friendfeed_btn" />

  40.                 <TextView
  41.                     android:layout_width="wrap_content"
  42.                     android:layout_height="wrap_content"
  43.                     android:layout_gravity="bottom|center"
  44.                     android:layout_marginBottom="6.0dip"
  45.                     android:text="动态"
  46.                     android:textColor="#ffffff"
  47.                     android:textSize="10sp" />
  48.             </FrameLayout>

  49.             <FrameLayout
  50.                 android:id="@+id/layout_myfeed"
  51.                 android:layout_width="fill_parent"
  52.                 android:layout_height="48dp"
  53.                 android:layout_weight="1" 
  54.                 android:background="@drawable/tab_btn_background">

  55.                 <ImageView
  56.                     android:id="@+id/image_myfeed"
  57.                     android:layout_width="wrap_content"
  58.                     android:layout_height="wrap_content"
  59.                     android:layout_gravity="top|center"
  60.                     android:layout_marginTop="1.0dip"
  61.                     android:src="@drawable/tab_myfeed_btn" />

  62.                 <TextView
  63.                     android:layout_width="wrap_content"
  64.                     android:layout_height="wrap_content"
  65.                     android:layout_gravity="bottom|center"
  66.                     android:layout_marginBottom="6.0dip"
  67.                     android:text="与我想关"
  68.                     android:textColor="#ffffff"
  69.                     android:textSize="10sp" />
  70.             </FrameLayout>

  71.             <FrameLayout
  72.                 android:layout_width="fill_parent"
  73.                 android:layout_height="wrap_content"
  74.                 android:layout_weight="1" >
  75.             </FrameLayout>

  76.             <FrameLayout
  77.                 android:id="@+id/layout_home"
  78.                 android:layout_width="fill_parent"
  79.                 android:layout_height="48dp"
  80.                 android:layout_weight="1" 
  81.                 android:background="@drawable/tab_btn_background">

  82.                 <ImageView
  83.                     android:id="@+id/image_home"
  84.                     android:layout_width="wrap_content"
  85.                     android:layout_height="wrap_content"
  86.                     android:layout_gravity="top|center"
  87.                     android:layout_marginTop="1.0dip"
  88.                     android:src="@drawable/tab_home_btn" />

  89.                 <TextView
  90.                     android:layout_width="wrap_content"
  91.                     android:layout_height="wrap_content"
  92.                     android:layout_gravity="bottom|center"
  93.                     android:layout_marginBottom="6.0dip"
  94.                     android:text="我的空间"
  95.                     android:textColor="#ffffff"
  96.                     android:textSize="10sp" />
  97.             </FrameLayout>

  98.             <FrameLayout
  99.                 android:id="@+id/layout_more"
  100.                 android:layout_width="fill_parent"
  101.                 android:layout_height="48dp"
  102.                 android:layout_weight="1" 
  103.                 android:background="@drawable/tab_btn_background">

  104.                 <ImageView
  105.                     android:id="@+id/image_more"
  106.                     android:layout_width="wrap_content"
  107.                     android:layout_height="wrap_content"
  108.                     android:layout_gravity="top|center"
  109.                     android:layout_marginTop="1.0dip"
  110.                     android:src="@drawable/tab_more_btn" />

  111.                 <TextView
  112.                     android:layout_width="wrap_content"
  113.                     android:layout_height="wrap_content"
  114.                     android:layout_gravity="bottom|center"
  115.                     android:layout_marginBottom="6.0dip"
  116.                     android:text="更多"
  117.                     android:textColor="#ffffff"
  118.                     android:textSize="10sp" />
  119.             </FrameLayout>
  120.         </LinearLayout>
  121.     </FrameLayout>

  122.     <ImageView
  123.         android:id="@+id/toggle_btn"
  124.         android:layout_width="wrap_content"
  125.         android:layout_height="wrap_content"
  126.         android:layout_alignParentBottom="true"
  127.         android:layout_centerHorizontal="true"
  128.         android:src="@drawable/toolbar_btn_normal" />

  129.     <ImageView
  130.         android:id="@+id/plus_btn"
  131.         android:layout_width="wrap_content"
  132.         android:layout_height="wrap_content"
  133.         android:layout_alignTop="@+id/frameLayout1"
  134.         android:layout_centerHorizontal="true"
  135.         android:layout_marginTop="6dip"
  136.         android:src="@drawable/toolbar_plus" />

  137. </RelativeLayout></span>
复制代码
我觉得这个布局界面是整个项目当中实现起来最复杂的地方,但是把思路理清楚了之后又觉得其实也没有那么复杂,详细说一下我实现的步骤:       <1> 最外层使用的是RelativeLayout,主要是为了容易摆放底部菜单栏的位置;       <2> 然后是一个FrameLayout,主要用来存放显示Fragment的内容,这里的ID取名为frame_content是用来替换Fragment对象的,在后面的代码中会用到;       <3> 最下面的底部菜单栏中外层使用了FrameLayout,之所以使用FrameLayout是为了让底部菜单栏中间的按钮也可以摆放进来,实现一种叠加的效果;       <4> 里面嵌套了LinearLayout,使用它是为了能够使用layout_weight属性,可以用来更好的摆放按钮,还可以实现自适应屏幕的效果(关于自适应屏幕的内容后面会有专题详细讲解)       <5> 最后里面又嵌套了一个FrameLayout,使用它可以很方便的实现图标在上文字在下的效果,最主要的原因是使用它可以很容易的再添加一个消息提醒的小图片(实际的开发中会用到,此项目中没有用到)
2、弹出菜单的布局界面,popwindow_layout.xml:
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent" >

  6.     <LinearLayout
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:layout_alignParentBottom="true"
  10.         android:background="@drawable/popwindow_bg"
  11.         android:orientation="vertical"
  12.         tools:ignore="UselessParent" >

  13.         <LinearLayout
  14.             android:layout_width="wrap_content"
  15.             android:layout_height="wrap_content"
  16.             android:layout_gravity="center_horizontal"
  17.             android:layout_marginTop="15dp"
  18.             android:orientation="horizontal" >

  19.             <LinearLayout
  20.                 android:layout_width="wrap_content"
  21.                 android:layout_height="wrap_content"
  22.                 android:layout_margin="5dp"
  23.                 android:layout_weight="1"
  24.                 android:orientation="vertical" >

  25.                 <ImageView
  26.                     android:layout_width="wrap_content"
  27.                     android:layout_height="wrap_content"
  28.                     android:layout_gravity="center_horizontal"
  29.                     android:layout_marginTop="1.0dip"
  30.                     android:src="@drawable/popwindow_write_btn" />

  31.                 <TextView
  32.                     android:layout_width="wrap_content"
  33.                     android:layout_height="wrap_content"
  34.                     android:layout_gravity="center_horizontal"
  35.                     android:layout_marginTop="5.0dip"
  36.                     android:shadowColor="#ff000000"
  37.                     android:shadowDx="1.0"
  38.                     android:shadowDy="1.0"
  39.                     android:shadowRadius="1.0"
  40.                     android:text="说说"
  41.                     android:textColor="#ffffffff"
  42.                     android:textSize="13.0dip" />
  43.             </LinearLayout>

  44.             <LinearLayout
  45.                 android:layout_width="wrap_content"
  46.                 android:layout_height="wrap_content"
  47.                 android:layout_margin="5dp"
  48.                 android:layout_weight="1"
  49.                 android:orientation="vertical" >

  50.                 <ImageView
  51.                     android:layout_width="wrap_content"
  52.                     android:layout_height="wrap_content"
  53.                     android:layout_gravity="center_horizontal"
  54.                     android:layout_marginTop="1.0dip"
  55.                     android:src="@drawable/popwindow_voice_btn" />

  56.                 <TextView
  57.                     android:layout_width="wrap_content"
  58.                     android:layout_height="wrap_content"
  59.                     android:layout_gravity="center_horizontal"
  60.                     android:layout_marginTop="5.0dip"
  61.                     android:shadowColor="#ff000000"
  62.                     android:shadowDx="1.0"
  63.                     android:shadowDy="1.0"
  64.                     android:shadowRadius="1.0"
  65.                     android:text="语音"
  66.                     android:textColor="#ffffffff"
  67.                     android:textSize="13.0dip" />
  68.             </LinearLayout>

  69.             <LinearLayout
  70.                 android:layout_width="wrap_content"
  71.                 android:layout_height="wrap_content"
  72.                 android:layout_margin="5dp"
  73.                 android:layout_weight="1"
  74.                 android:orientation="vertical" >

  75.                 <ImageView
  76.                     android:layout_width="wrap_content"
  77.                     android:layout_height="wrap_content"
  78.                     android:layout_gravity="center_horizontal"
  79.                     android:layout_marginTop="1.0dip"
  80.                     android:src="@drawable/popwindow_camera_btn" />

  81.                 <TextView
  82.                     android:layout_width="wrap_content"
  83.                     android:layout_height="wrap_content"
  84.                     android:layout_gravity="center_horizontal"
  85.                     android:layout_marginTop="5.0dip"
  86.                     android:shadowColor="#ff000000"
  87.                     android:shadowDx="1.0"
  88.                     android:shadowDy="1.0"
  89.                     android:shadowRadius="1.0"
  90.                     android:text="照片"
  91.                     android:textColor="#ffffffff"
  92.                     android:textSize="13.0dip" />
  93.             </LinearLayout>

  94.             <LinearLayout
  95.                 android:layout_width="wrap_content"
  96.                 android:layout_height="wrap_content"
  97.                 android:layout_margin="5dp"
  98.                 android:layout_weight="1"
  99.                 android:orientation="vertical" >

  100.                 <ImageView
  101.                     android:layout_width="wrap_content"
  102.                     android:layout_height="wrap_content"
  103.                     android:layout_gravity="center_horizontal"
  104.                     android:layout_marginTop="1.0dip"
  105.                     android:src="@drawable/popwindow_picture_btn" />

  106.                 <TextView
  107.                     android:layout_width="wrap_content"
  108.                     android:layout_height="wrap_content"
  109.                     android:layout_gravity="center_horizontal"
  110.                     android:layout_marginTop="5.0dip"
  111.                     android:shadowColor="#ff000000"
  112.                     android:shadowDx="1.0"
  113.                     android:shadowDy="1.0"
  114.                     android:shadowRadius="1.0"
  115.                     android:text="视频"
  116.                     android:textColor="#ffffffff"
  117.                     android:textSize="13.0dip" />
  118.             </LinearLayout>

  119.             <LinearLayout
  120.                 android:layout_width="wrap_content"
  121.                 android:layout_height="wrap_content"
  122.                 android:layout_margin="5dp"
  123.                 android:layout_weight="1"
  124.                 android:orientation="vertical" >

  125.                 <ImageView
  126.                     android:layout_width="wrap_content"
  127.                     android:layout_height="wrap_content"
  128.                     android:layout_gravity="center_horizontal"
  129.                     android:layout_marginTop="1.0dip"
  130.                     android:src="@drawable/popwindow_sign_btn" />

  131.                 <TextView
  132.                     android:layout_width="wrap_content"
  133.                     android:layout_height="wrap_content"
  134.                     android:layout_gravity="center_horizontal"
  135.                     android:layout_marginTop="5.0dip"
  136.                     android:shadowColor="#ff000000"
  137.                     android:shadowDx="1.0"
  138.                     android:shadowDy="1.0"
  139.                     android:shadowRadius="1.0"
  140.                     android:text="签到"
  141.                     android:textColor="#ffffffff"
  142.                     android:textSize="13.0dip" />
  143.             </LinearLayout>
  144.         </LinearLayout>
  145.     </LinearLayout>

  146. </RelativeLayout></span>
复制代码
3、其中一个Fragment布局页面,fragment_1.xml:
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" >

  5.     <ImageView
  6.         android:id="@+id/imageview"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.         android:scaleType="fitCenter"
  10.         android:src="@drawable/xianjian01" >
  11.     </ImageView>

  12. </LinearLayout></span>
复制代码
4、自定义按钮的资源文件,由于比较简单,就列出其中一个,tab_friendfeed_btn.xml:
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">

  3.     <item android:drawable="@drawable/toolbar_friendfeed_pressed" android:state_selected="true"/>
  4.     <item android:drawable="@drawable/toolbar_friendfeed_normal"/>

  5. </selector></span>
复制代码
5、主Activity界面,MainActivity.java:
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab03;

  2. import android.content.Context;
  3. import android.graphics.drawable.BitmapDrawable;
  4. import android.os.Bundle;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.support.v4.app.FragmentTransaction;
  7. import android.view.Gravity;
  8. import android.view.LayoutInflater;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.view.View.OnTouchListener;
  13. import android.widget.FrameLayout;
  14. import android.widget.ImageView;
  15. import android.widget.LinearLayout;
  16. import android.widget.PopupWindow;
  17. import android.widget.PopupWindow.OnDismissListener;

  18. public class MainActivity extends FragmentActivity implements OnClickListener{
  19.         //定义Fragment页面
  20.         private FragmentPage1 fragmentPage1;
  21.         private FragmentPage2 fragmentPage2;
  22.         private FragmentPage3 fragmentPage3;
  23.         private FragmentPage4 fragmentPage4;
  24.                 
  25.         //定义布局对象
  26.         private FrameLayout friendfeedFl,myfeedFl,homeFl,moreFl;
  27.         
  28.         //定义图片组件对象
  29.         private ImageView friendfeedIv,myfeedIv,homeIv,moreIv;
  30.         
  31.         //定义按钮图片组件
  32.         private ImageView toggleImageView,plusImageView;
  33.         
  34.         //定义PopupWindow
  35.         private PopupWindow popWindow;
  36.            
  37.         @Override
  38.         protected void onCreate(Bundle savedInstanceState) {
  39.                 super.onCreate(savedInstanceState);
  40.                 setContentView(R.layout.activity_main);
  41.                 
  42.                 initView();
  43.                 
  44.                 initData();
  45.                 
  46.                 //初始化默认为选中点击了“动态”按钮
  47.                 clickFriendfeedBtn();
  48.         }

  49.         /**
  50.          * 初始化组件
  51.          */
  52.         private void initView(){                        
  53.                 //实例化布局对象
  54.                 friendfeedFl = (FrameLayout)findViewById(R.id.layout_friendfeed);
  55.                 myfeedFl = (FrameLayout)findViewById(R.id.layout_myfeed);
  56.                 homeFl = (FrameLayout)findViewById(R.id.layout_home);
  57.                 moreFl = (FrameLayout)findViewById(R.id.layout_more);
  58.                 
  59.                 //实例化图片组件对象
  60.                 friendfeedIv = (ImageView)findViewById(R.id.image_friendfeed);
  61.                 myfeedIv = (ImageView)findViewById(R.id.image_myfeed);
  62.                 homeIv = (ImageView)findViewById(R.id.image_home);
  63.                 moreIv = (ImageView)findViewById(R.id.image_more);
  64.                 
  65.                 //实例化按钮图片组件
  66.                 toggleImageView = (ImageView)findViewById(R.id.toggle_btn);
  67.                 plusImageView = (ImageView)findViewById(R.id.plus_btn);
  68.         }

  69.         /**
  70.          * 初始化数据
  71.          */
  72.         private void initData(){
  73.                 //给布局对象设置监听
  74.                 friendfeedFl.setOnClickListener(this);
  75.                 myfeedFl.setOnClickListener(this);
  76.                 homeFl.setOnClickListener(this);
  77.                 moreFl.setOnClickListener(this);

  78.                 //给按钮图片设置监听
  79.                 toggleImageView.setOnClickListener(this);
  80.         }

  81.         @Override
  82.         public void onClick(View v) {
  83.                 switch (v.getId()) {
  84.                 //点击动态按钮
  85.                 case R.id.layout_friendfeed:
  86.                         clickFriendfeedBtn();
  87.                         break;
  88.                 //点击与我相关按钮
  89.                 case R.id.layout_myfeed:
  90.                         clickMyfeedBtn();
  91.                         break;
  92.                 //点击我的空间按钮
  93.                 case R.id.layout_home:
  94.                         clickHomeBtn();
  95.                         break;
  96.                 //点击更多按钮
  97.                 case R.id.layout_more:
  98.                         clickMoreBtn();
  99.                         break;
  100.                 //点击中间按钮
  101.                 case R.id.toggle_btn:
  102.                         clickToggleBtn();
  103.                         break;                
  104.                 }
  105.         }
  106.         
  107.         /**
  108.          * 显示PopupWindow弹出菜单
  109.          */
  110.         private void showPopupWindow(View parent){
  111.                 if (popWindow == null) {                
  112.                         LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  113.                         View view = layoutInflater.inflate(R.layout.popwindow_layout,null);
  114.                         // 创建一个PopuWidow对象
  115.                         popWindow = new PopupWindow(view,LinearLayout.LayoutParams.FILL_PARENT, 200);
  116.                 }        
  117.                 // 使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法
  118.                 popWindow.setFocusable(true);
  119.                 // 设置允许在外点击消失
  120.                 popWindow.setOutsideTouchable(true);
  121.                 // 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
  122.                 popWindow.setBackgroundDrawable(new BitmapDrawable());                
  123.                 //设置菜单显示的位置
  124.                 popWindow.showAsDropDown(parent, Gravity.CENTER, 0);

  125.                 //监听菜单的关闭事件
  126.                 popWindow.setOnDismissListener(new OnDismissListener() {                        
  127.                         @Override
  128.                         public void onDismiss() {
  129.                                 //改变显示的按钮图片为正常状态
  130.                                 changeButtonImage();                                
  131.                         }
  132.                 });
  133.                 
  134.                 //监听触屏事件
  135.                 popWindow.setTouchInterceptor(new OnTouchListener() {
  136.                         public boolean onTouch(View view, MotionEvent event) {                        
  137.                                 //改变显示的按钮图片为正常状态
  138.                                 changeButtonImage();

  139.                                 return false;                                                                
  140.                         }
  141.                 });
  142.         }        
  143.         
  144.         /**
  145.          * 点击了“动态”按钮
  146.          */
  147.         private void clickFriendfeedBtn(){
  148.                 //实例化Fragment页面
  149.                 fragmentPage1 = new FragmentPage1();
  150.                 //得到Fragment事务管理器
  151.                 FragmentTransaction        fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
  152.                 //替换当前的页面
  153.                 fragmentTransaction.replace(R.id.frame_content, fragmentPage1);
  154.                 //事务管理提交
  155.                 fragmentTransaction.commit();
  156.                 
  157.                 friendfeedFl.setSelected(true);
  158.                 friendfeedIv.setSelected(true);
  159.                 
  160.                 myfeedFl.setSelected(false);
  161.                 myfeedIv.setSelected(false);
  162.                 
  163.                 homeFl.setSelected(false);
  164.                 homeIv.setSelected(false);
  165.                 
  166.                 moreFl.setSelected(false);
  167.                 moreIv.setSelected(false);
  168.         }
  169.         
  170.         /**
  171.          * 点击了“与我相关”按钮
  172.          */
  173.         private void clickMyfeedBtn(){
  174.                 // 实例化Fragment页面
  175.                 fragmentPage2 = new FragmentPage2();
  176.                 // 得到Fragment事务管理器
  177.                 FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
  178.                 // 替换当前的页面
  179.                 fragmentTransaction.replace(R.id.frame_content, fragmentPage2);
  180.                 // 事务管理提交
  181.                 fragmentTransaction.commit();
  182.                 
  183.                 friendfeedFl.setSelected(false);
  184.                 friendfeedIv.setSelected(false);
  185.                 
  186.                 myfeedFl.setSelected(true);
  187.                 myfeedIv.setSelected(true);
  188.                 
  189.                 homeFl.setSelected(false);
  190.                 homeIv.setSelected(false);
  191.                 
  192.                 moreFl.setSelected(false);
  193.                 moreIv.setSelected(false);
  194.         }
  195.         
  196.         /**
  197.          * 点击了“我的空间”按钮
  198.          */
  199.         private void clickHomeBtn(){
  200.                 // 实例化Fragment页面
  201.                 fragmentPage3 = new FragmentPage3();
  202.                 // 得到Fragment事务管理器
  203.                 FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
  204.                 // 替换当前的页面
  205.                 fragmentTransaction.replace(R.id.frame_content, fragmentPage3);
  206.                 // 事务管理提交
  207.                 fragmentTransaction.commit();
  208.                                 
  209.                 friendfeedFl.setSelected(false);
  210.                 friendfeedIv.setSelected(false);
  211.                 
  212.                 myfeedFl.setSelected(false);
  213.                 myfeedIv.setSelected(false);
  214.                 
  215.                 homeFl.setSelected(true);
  216.                 homeIv.setSelected(true);
  217.                 
  218.                 moreFl.setSelected(false);
  219.                 moreIv.setSelected(false);
  220.         }
  221.         
  222.         /**
  223.          * 点击了“更多”按钮
  224.          */
  225.         private void clickMoreBtn(){
  226.                 // 实例化Fragment页面
  227.                 fragmentPage4 = new FragmentPage4();
  228.                 // 得到Fragment事务管理器
  229.                 FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
  230.                 // 替换当前的页面
  231.                 fragmentTransaction.replace(R.id.frame_content, fragmentPage4);
  232.                 // 事务管理提交
  233.                 fragmentTransaction.commit();
  234.                 
  235.                 friendfeedFl.setSelected(false);
  236.                 friendfeedIv.setSelected(false);
  237.                 
  238.                 myfeedFl.setSelected(false);
  239.                 myfeedIv.setSelected(false);
  240.                 
  241.                 homeFl.setSelected(false);
  242.                 homeIv.setSelected(false);
  243.                 
  244.                 moreFl.setSelected(true);
  245.                 moreIv.setSelected(true);
  246.         }
  247.         
  248.         /**
  249.          * 点击了中间按钮
  250.          */
  251.         private void clickToggleBtn(){                
  252.                 showPopupWindow(toggleImageView);
  253.                 //改变按钮显示的图片为按下时的状态
  254.                 plusImageView.setImageResource(R.drawable.toolbar_plusback);
  255.                 toggleImageView.setImageResource(R.drawable.toolbar_btn_pressed);
  256.         }
  257.         
  258.         /**
  259.          * 改变显示的按钮图片为正常状态
  260.          */
  261.         private void changeButtonImage(){
  262.                 plusImageView.setImageResource(R.drawable.toolbar_plus);
  263.                 toggleImageView.setImageResource(R.drawable.toolbar_btn_normal);
  264.         }
  265.                 
  266. }</span><span style="font-size:18px;">
  267. </span>
复制代码
6、Fragment其中一个页面,FragmentPage1.java:
  1. package com.yangyu.mycustomtab03;

  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;

  7. public class FragmentPage1 extends Fragment{

  8.         @Override
  9.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10.                         Bundle savedInstanceState) {
  11.                 
  12.                 return inflater.inflate(R.layout.fragment_1, null);                
  13.         }        
  14. }
复制代码
到这里整个项目就基本上讲完了,大家还可以为此项目继续完善下去,实现点击菜单选项后实现的效果以及顶部标题栏的实现。对于这个项目,博主还有一个地方不明白,就是针对此项目,在弹出菜单后,有什么好的简单一点的方法将背景置为灰色?我也试了一些方法,但是效果都不是特别的理想,希望读者有什么想法可以给博主留言,大家可以互相交流学习一下。

源码下载地址: http://download.csdn.net/my/uploads/1

0 0
原创粉丝点击