底部菜单栏(二)Fragment+PopupWindow仿QQ空间最新版底部菜单栏

转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9023451         


     在今天的这篇文章当中,我依然会以实战加理论结合的方式教大家如何设计出自己觉得很炫的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.   
  6.     <FrameLayout  
  7.         android:id="@+id/frame_content"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_above="@+id/frameLayout1"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentRight="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:background="#ffffff" >  
  15.     </FrameLayout>  
  16.   
  17.     <FrameLayout  
  18.         android:id="@+id/frameLayout1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_alignParentLeft="true"  
  23.         android:background="@drawable/toolbar_bg_normal" >  
  24.   
  25.         <LinearLayout  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="fill_parent"  
  28.             android:layout_marginTop="1dp"  
  29.             android:gravity="center_horizontal" >  
  30.   
  31.             <FrameLayout  
  32.                 android:id="@+id/layout_friendfeed"  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="48dp"  
  35.                 android:layout_weight="1"   
  36.                 android:background="@drawable/tab_btn_background">  
  37.   
  38.                 <ImageView  
  39.                     android:id="@+id/image_friendfeed"  
  40.                     android:layout_width="wrap_content"  
  41.                     android:layout_height="wrap_content"  
  42.                     android:layout_gravity="top|center"  
  43.                     android:layout_marginTop="1.0dip"  
  44.                     android:src="@drawable/tab_friendfeed_btn" />  
  45.   
  46.                 <TextView  
  47.                     android:layout_width="wrap_content"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:layout_gravity="bottom|center"  
  50.                     android:layout_marginBottom="6.0dip"  
  51.                     android:text="动态"  
  52.                     android:textColor="#ffffff"  
  53.                     android:textSize="10sp" />  
  54.             </FrameLayout>  
  55.   
  56.             <FrameLayout  
  57.                 android:id="@+id/layout_myfeed"  
  58.                 android:layout_width="fill_parent"  
  59.                 android:layout_height="48dp"  
  60.                 android:layout_weight="1"   
  61.                 android:background="@drawable/tab_btn_background">  
  62.   
  63.                 <ImageView  
  64.                     android:id="@+id/image_myfeed"  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:layout_gravity="top|center"  
  68.                     android:layout_marginTop="1.0dip"  
  69.                     android:src="@drawable/tab_myfeed_btn" />  
  70.   
  71.                 <TextView  
  72.                     android:layout_width="wrap_content"  
  73.                     android:layout_height="wrap_content"  
  74.                     android:layout_gravity="bottom|center"  
  75.                     android:layout_marginBottom="6.0dip"  
  76.                     android:text="与我想关"  
  77.                     android:textColor="#ffffff"  
  78.                     android:textSize="10sp" />  
  79.             </FrameLayout>  
  80.   
  81.             <FrameLayout  
  82.                 android:layout_width="fill_parent"  
  83.                 android:layout_height="wrap_content"  
  84.                 android:layout_weight="1" >  
  85.             </FrameLayout>  
  86.   
  87.             <FrameLayout  
  88.                 android:id="@+id/layout_home"  
  89.                 android:layout_width="fill_parent"  
  90.                 android:layout_height="48dp"  
  91.                 android:layout_weight="1"   
  92.                 android:background="@drawable/tab_btn_background">  
  93.   
  94.                 <ImageView  
  95.                     android:id="@+id/image_home"  
  96.                     android:layout_width="wrap_content"  
  97.                     android:layout_height="wrap_content"  
  98.                     android:layout_gravity="top|center"  
  99.                     android:layout_marginTop="1.0dip"  
  100.                     android:src="@drawable/tab_home_btn" />  
  101.   
  102.                 <TextView  
  103.                     android:layout_width="wrap_content"  
  104.                     android:layout_height="wrap_content"  
  105.                     android:layout_gravity="bottom|center"  
  106.                     android:layout_marginBottom="6.0dip"  
  107.                     android:text="我的空间"  
  108.                     android:textColor="#ffffff"  
  109.                     android:textSize="10sp" />  
  110.             </FrameLayout>  
  111.   
  112.             <FrameLayout  
  113.                 android:id="@+id/layout_more"  
  114.                 android:layout_width="fill_parent"  
  115.                 android:layout_height="48dp"  
  116.                 android:layout_weight="1"   
  117.                 android:background="@drawable/tab_btn_background">  
  118.   
  119.                 <ImageView  
  120.                     android:id="@+id/image_more"  
  121.                     android:layout_width="wrap_content"  
  122.                     android:layout_height="wrap_content"  
  123.                     android:layout_gravity="top|center"  
  124.                     android:layout_marginTop="1.0dip"  
  125.                     android:src="@drawable/tab_more_btn" />  
  126.   
  127.                 <TextView  
  128.                     android:layout_width="wrap_content"  
  129.                     android:layout_height="wrap_content"  
  130.                     android:layout_gravity="bottom|center"  
  131.                     android:layout_marginBottom="6.0dip"  
  132.                     android:text="更多"  
  133.                     android:textColor="#ffffff"  
  134.                     android:textSize="10sp" />  
  135.             </FrameLayout>  
  136.         </LinearLayout>  
  137.     </FrameLayout>  
  138.   
  139.     <ImageView  
  140.         android:id="@+id/toggle_btn"  
  141.         android:layout_width="wrap_content"  
  142.         android:layout_height="wrap_content"  
  143.         android:layout_alignParentBottom="true"  
  144.         android:layout_centerHorizontal="true"  
  145.         android:src="@drawable/toolbar_btn_normal" />  
  146.   
  147.     <ImageView  
  148.         android:id="@+id/plus_btn"  
  149.         android:layout_width="wrap_content"  
  150.         android:layout_height="wrap_content"  
  151.         android:layout_alignTop="@+id/frameLayout1"  
  152.         android:layout_centerHorizontal="true"  
  153.         android:layout_marginTop="6dip"  
  154.         android:src="@drawable/toolbar_plus" />  
  155.   
  156. </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.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentBottom="true"  
  11.         android:background="@drawable/popwindow_bg"  
  12.         android:orientation="vertical"  
  13.         tools:ignore="UselessParent" >  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_gravity="center_horizontal"  
  19.             android:layout_marginTop="15dp"  
  20.             android:orientation="horizontal" >  
  21.   
  22.             <LinearLayout  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:layout_margin="5dp"  
  26.                 android:layout_weight="1"  
  27.                 android:orientation="vertical" >  
  28.   
  29.                 <ImageView  
  30.                     android:layout_width="wrap_content"  
  31.                     android:layout_height="wrap_content"  
  32.                     android:layout_gravity="center_horizontal"  
  33.                     android:layout_marginTop="1.0dip"  
  34.                     android:src="@drawable/popwindow_write_btn" />  
  35.   
  36.                 <TextView  
  37.                     android:layout_width="wrap_content"  
  38.                     android:layout_height="wrap_content"  
  39.                     android:layout_gravity="center_horizontal"  
  40.                     android:layout_marginTop="5.0dip"  
  41.                     android:shadowColor="#ff000000"  
  42.                     android:shadowDx="1.0"  
  43.                     android:shadowDy="1.0"  
  44.                     android:shadowRadius="1.0"  
  45.                     android:text="说说"  
  46.                     android:textColor="#ffffffff"  
  47.                     android:textSize="13.0dip" />  
  48.             </LinearLayout>  
  49.   
  50.             <LinearLayout  
  51.                 android:layout_width="wrap_content"  
  52.                 android:layout_height="wrap_content"  
  53.                 android:layout_margin="5dp"  
  54.                 android:layout_weight="1"  
  55.                 android:orientation="vertical" >  
  56.   
  57.                 <ImageView  
  58.                     android:layout_width="wrap_content"  
  59.                     android:layout_height="wrap_content"  
  60.                     android:layout_gravity="center_horizontal"  
  61.                     android:layout_marginTop="1.0dip"  
  62.                     android:src="@drawable/popwindow_voice_btn" />  
  63.   
  64.                 <TextView  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:layout_gravity="center_horizontal"  
  68.                     android:layout_marginTop="5.0dip"  
  69.                     android:shadowColor="#ff000000"  
  70.                     android:shadowDx="1.0"  
  71.                     android:shadowDy="1.0"  
  72.                     android:shadowRadius="1.0"  
  73.                     android:text="语音"  
  74.                     android:textColor="#ffffffff"  
  75.                     android:textSize="13.0dip" />  
  76.             </LinearLayout>  
  77.   
  78.             <LinearLayout  
  79.                 android:layout_width="wrap_content"  
  80.                 android:layout_height="wrap_content"  
  81.                 android:layout_margin="5dp"  
  82.                 android:layout_weight="1"  
  83.                 android:orientation="vertical" >  
  84.   
  85.                 <ImageView  
  86.                     android:layout_width="wrap_content"  
  87.                     android:layout_height="wrap_content"  
  88.                     android:layout_gravity="center_horizontal"  
  89.                     android:layout_marginTop="1.0dip"  
  90.                     android:src="@drawable/popwindow_camera_btn" />  
  91.   
  92.                 <TextView  
  93.                     android:layout_width="wrap_content"  
  94.                     android:layout_height="wrap_content"  
  95.                     android:layout_gravity="center_horizontal"  
  96.                     android:layout_marginTop="5.0dip"  
  97.                     android:shadowColor="#ff000000"  
  98.                     android:shadowDx="1.0"  
  99.                     android:shadowDy="1.0"  
  100.                     android:shadowRadius="1.0"  
  101.                     android:text="照片"  
  102.                     android:textColor="#ffffffff"  
  103.                     android:textSize="13.0dip" />  
  104.             </LinearLayout>  
  105.   
  106.             <LinearLayout  
  107.                 android:layout_width="wrap_content"  
  108.                 android:layout_height="wrap_content"  
  109.                 android:layout_margin="5dp"  
  110.                 android:layout_weight="1"  
  111.                 android:orientation="vertical" >  
  112.   
  113.                 <ImageView  
  114.                     android:layout_width="wrap_content"  
  115.                     android:layout_height="wrap_content"  
  116.                     android:layout_gravity="center_horizontal"  
  117.                     android:layout_marginTop="1.0dip"  
  118.                     android:src="@drawable/popwindow_picture_btn" />  
  119.   
  120.                 <TextView  
  121.                     android:layout_width="wrap_content"  
  122.                     android:layout_height="wrap_content"  
  123.                     android:layout_gravity="center_horizontal"  
  124.                     android:layout_marginTop="5.0dip"  
  125.                     android:shadowColor="#ff000000"  
  126.                     android:shadowDx="1.0"  
  127.                     android:shadowDy="1.0"  
  128.                     android:shadowRadius="1.0"  
  129.                     android:text="视频"  
  130.                     android:textColor="#ffffffff"  
  131.                     android:textSize="13.0dip" />  
  132.             </LinearLayout>  
  133.   
  134.             <LinearLayout  
  135.                 android:layout_width="wrap_content"  
  136.                 android:layout_height="wrap_content"  
  137.                 android:layout_margin="5dp"  
  138.                 android:layout_weight="1"  
  139.                 android:orientation="vertical" >  
  140.   
  141.                 <ImageView  
  142.                     android:layout_width="wrap_content"  
  143.                     android:layout_height="wrap_content"  
  144.                     android:layout_gravity="center_horizontal"  
  145.                     android:layout_marginTop="1.0dip"  
  146.                     android:src="@drawable/popwindow_sign_btn" />  
  147.   
  148.                 <TextView  
  149.                     android:layout_width="wrap_content"  
  150.                     android:layout_height="wrap_content"  
  151.                     android:layout_gravity="center_horizontal"  
  152.                     android:layout_marginTop="5.0dip"  
  153.                     android:shadowColor="#ff000000"  
  154.                     android:shadowDx="1.0"  
  155.                     android:shadowDy="1.0"  
  156.                     android:shadowRadius="1.0"  
  157.                     android:text="签到"  
  158.                     android:textColor="#ffffffff"  
  159.                     android:textSize="13.0dip" />  
  160.             </LinearLayout>  
  161.         </LinearLayout>  
  162.     </LinearLayout>  
  163.   
  164. </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.   
  6.     <ImageView  
  7.         android:id="@+id/imageview"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:scaleType="fitCenter"  
  11.         android:src="@drawable/xianjian01" >  
  12.     </ImageView>  
  13.   
  14. </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.   
  4.     <item android:drawable="@drawable/toolbar_friendfeed_pressed" android:state_selected="true"/>  
  5.     <item android:drawable="@drawable/toolbar_friendfeed_normal"/>  
  6.   
  7. </selector></span>  
5、主Activity界面,MainActivity.java:
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab03;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.BitmapDrawable;  
  5. import android.os.Bundle;  
  6. import android.support.v4.app.FragmentActivity;  
  7. import android.support.v4.app.FragmentTransaction;  
  8. import android.view.Gravity;  
  9. import android.view.LayoutInflater;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.view.View.OnTouchListener;  
  14. import android.widget.FrameLayout;  
  15. import android.widget.ImageView;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.PopupWindow;  
  18. import android.widget.PopupWindow.OnDismissListener;  
  19.   
  20. public class MainActivity extends FragmentActivity implements OnClickListener{  
  21.     //定义Fragment页面  
  22.     private FragmentPage1 fragmentPage1;  
  23.     private FragmentPage2 fragmentPage2;  
  24.     private FragmentPage3 fragmentPage3;  
  25.     private FragmentPage4 fragmentPage4;  
  26.           
  27.     //定义布局对象  
  28.     private FrameLayout friendfeedFl,myfeedFl,homeFl,moreFl;  
  29.       
  30.     //定义图片组件对象  
  31.     private ImageView friendfeedIv,myfeedIv,homeIv,moreIv;  
  32.       
  33.     //定义按钮图片组件  
  34.     private ImageView toggleImageView,plusImageView;  
  35.       
  36.     //定义PopupWindow  
  37.     private PopupWindow popWindow;  
  38.          
  39.     @Override  
  40.     protected void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.activity_main);  
  43.           
  44.         initView();  
  45.           
  46.         initData();  
  47.           
  48.         //初始化默认为选中点击了“动态”按钮  
  49.         clickFriendfeedBtn();  
  50.     }  
  51.   
  52.     /** 
  53.      * 初始化组件 
  54.      */  
  55.     private void initView(){              
  56.         //实例化布局对象  
  57.         friendfeedFl = (FrameLayout)findViewById(R.id.layout_friendfeed);  
  58.         myfeedFl = (FrameLayout)findViewById(R.id.layout_myfeed);  
  59.         homeFl = (FrameLayout)findViewById(R.id.layout_home);  
  60.         moreFl = (FrameLayout)findViewById(R.id.layout_more);  
  61.           
  62.         //实例化图片组件对象  
  63.         friendfeedIv = (ImageView)findViewById(R.id.image_friendfeed);  
  64.         myfeedIv = (ImageView)findViewById(R.id.image_myfeed);  
  65.         homeIv = (ImageView)findViewById(R.id.image_home);  
  66.         moreIv = (ImageView)findViewById(R.id.image_more);  
  67.           
  68.         //实例化按钮图片组件  
  69.         toggleImageView = (ImageView)findViewById(R.id.toggle_btn);  
  70.         plusImageView = (ImageView)findViewById(R.id.plus_btn);  
  71.     }  
  72.   
  73.     /** 
  74.      * 初始化数据 
  75.      */  
  76.     private void initData(){  
  77.         //给布局对象设置监听  
  78.         friendfeedFl.setOnClickListener(this);  
  79.         myfeedFl.setOnClickListener(this);  
  80.         homeFl.setOnClickListener(this);  
  81.         moreFl.setOnClickListener(this);  
  82.   
  83.         //给按钮图片设置监听  
  84.         toggleImageView.setOnClickListener(this);  
  85.     }  
  86.   
  87.     @Override  
  88.     public void onClick(View v) {  
  89.         switch (v.getId()) {  
  90.         //点击动态按钮  
  91.         case R.id.layout_friendfeed:  
  92.             clickFriendfeedBtn();  
  93.             break;  
  94.         //点击与我相关按钮  
  95.         case R.id.layout_myfeed:  
  96.             clickMyfeedBtn();  
  97.             break;  
  98.         //点击我的空间按钮  
  99.         case R.id.layout_home:  
  100.             clickHomeBtn();  
  101.             break;  
  102.         //点击更多按钮  
  103.         case R.id.layout_more:  
  104.             clickMoreBtn();  
  105.             break;  
  106.         //点击中间按钮  
  107.         case R.id.toggle_btn:  
  108.             clickToggleBtn();  
  109.             break;        
  110.         }  
  111.     }  
  112.       
  113.     /** 
  114.      * 显示PopupWindow弹出菜单 
  115.      */  
  116.     private void showPopupWindow(View parent){  
  117.         if (popWindow == null) {          
  118.             LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  119.   
  120.             View view = layoutInflater.inflate(R.layout.popwindow_layout,null);  
  121.             // 创建一个PopuWidow对象  
  122.             popWindow = new PopupWindow(view,LinearLayout.LayoutParams.FILL_PARENT, 200);  
  123.         }     
  124.         // 使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法  
  125.         popWindow.setFocusable(true);  
  126.         // 设置允许在外点击消失  
  127.         popWindow.setOutsideTouchable(true);  
  128.         // 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
  129.         popWindow.setBackgroundDrawable(new BitmapDrawable());        
  130.         //设置菜单显示的位置  
  131.         popWindow.showAsDropDown(parent, Gravity.CENTER, 0);  
  132.   
  133.         //监听菜单的关闭事件  
  134.         popWindow.setOnDismissListener(new OnDismissListener() {              
  135.             @Override  
  136.             public void onDismiss() {  
  137.                 //改变显示的按钮图片为正常状态  
  138.                 changeButtonImage();                  
  139.             }  
  140.         });  
  141.           
  142.         //监听触屏事件  
  143.         popWindow.setTouchInterceptor(new OnTouchListener() {  
  144.             public boolean onTouch(View view, MotionEvent event) {            
  145.                 //改变显示的按钮图片为正常状态  
  146.                 changeButtonImage();  
  147.   
  148.                 return false;                                 
  149.             }  
  150.         });  
  151.     }     
  152.       
  153.     /** 
  154.      * 点击了“动态”按钮 
  155.      */  
  156.     private void clickFriendfeedBtn(){  
  157.         //实例化Fragment页面  
  158.         fragmentPage1 = new FragmentPage1();  
  159.         //得到Fragment事务管理器  
  160.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  161.         //替换当前的页面  
  162.         fragmentTransaction.replace(R.id.frame_content, fragmentPage1);  
  163.         //事务管理提交  
  164.         fragmentTransaction.commit();  
  165.           
  166.         friendfeedFl.setSelected(true);  
  167.         friendfeedIv.setSelected(true);  
  168.           
  169.         myfeedFl.setSelected(false);  
  170.         myfeedIv.setSelected(false);  
  171.           
  172.         homeFl.setSelected(false);  
  173.         homeIv.setSelected(false);  
  174.           
  175.         moreFl.setSelected(false);  
  176.         moreIv.setSelected(false);  
  177.     }  
  178.       
  179.     /** 
  180.      * 点击了“与我相关”按钮 
  181.      */  
  182.     private void clickMyfeedBtn(){  
  183.         // 实例化Fragment页面  
  184.         fragmentPage2 = new FragmentPage2();  
  185.         // 得到Fragment事务管理器  
  186.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  187.         // 替换当前的页面  
  188.         fragmentTransaction.replace(R.id.frame_content, fragmentPage2);  
  189.         // 事务管理提交  
  190.         fragmentTransaction.commit();  
  191.           
  192.         friendfeedFl.setSelected(false);  
  193.         friendfeedIv.setSelected(false);  
  194.           
  195.         myfeedFl.setSelected(true);  
  196.         myfeedIv.setSelected(true);  
  197.           
  198.         homeFl.setSelected(false);  
  199.         homeIv.setSelected(false);  
  200.           
  201.         moreFl.setSelected(false);  
  202.         moreIv.setSelected(false);  
  203.     }  
  204.       
  205.     /** 
  206.      * 点击了“我的空间”按钮 
  207.      */  
  208.     private void clickHomeBtn(){  
  209.         // 实例化Fragment页面  
  210.         fragmentPage3 = new FragmentPage3();  
  211.         // 得到Fragment事务管理器  
  212.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  213.         // 替换当前的页面  
  214.         fragmentTransaction.replace(R.id.frame_content, fragmentPage3);  
  215.         // 事务管理提交  
  216.         fragmentTransaction.commit();  
  217.                   
  218.         friendfeedFl.setSelected(false);  
  219.         friendfeedIv.setSelected(false);  
  220.           
  221.         myfeedFl.setSelected(false);  
  222.         myfeedIv.setSelected(false);  
  223.           
  224.         homeFl.setSelected(true);  
  225.         homeIv.setSelected(true);  
  226.           
  227.         moreFl.setSelected(false);  
  228.         moreIv.setSelected(false);  
  229.     }  
  230.       
  231.     /** 
  232.      * 点击了“更多”按钮 
  233.      */  
  234.     private void clickMoreBtn(){  
  235.         // 实例化Fragment页面  
  236.         fragmentPage4 = new FragmentPage4();  
  237.         // 得到Fragment事务管理器  
  238.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  239.         // 替换当前的页面  
  240.         fragmentTransaction.replace(R.id.frame_content, fragmentPage4);  
  241.         // 事务管理提交  
  242.         fragmentTransaction.commit();  
  243.           
  244.         friendfeedFl.setSelected(false);  
  245.         friendfeedIv.setSelected(false);  
  246.           
  247.         myfeedFl.setSelected(false);  
  248.         myfeedIv.setSelected(false);  
  249.           
  250.         homeFl.setSelected(false);  
  251.         homeIv.setSelected(false);  
  252.           
  253.         moreFl.setSelected(true);  
  254.         moreIv.setSelected(true);  
  255.     }  
  256.       
  257.     /** 
  258.      * 点击了中间按钮 
  259.      */  
  260.     private void clickToggleBtn(){        
  261.         showPopupWindow(toggleImageView);  
  262.         //改变按钮显示的图片为按下时的状态  
  263.         plusImageView.setImageResource(R.drawable.toolbar_plusback);  
  264.         toggleImageView.setImageResource(R.drawable.toolbar_btn_pressed);  
  265.     }  
  266.       
  267.     /** 
  268.      * 改变显示的按钮图片为正常状态 
  269.      */  
  270.     private void changeButtonImage(){  
  271.         plusImageView.setImageResource(R.drawable.toolbar_plus);  
  272.         toggleImageView.setImageResource(R.drawable.toolbar_btn_normal);  
  273.     }  
  274.           
  275. }</span><span style="font-size:18px;">  
  276. </span>  
6、Fragment其中一个页面,FragmentPage1.java:
  1. package com.yangyu.mycustomtab03;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class FragmentPage1 extends Fragment{  
  10.   
  11.     @Override  
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  13.             Bundle savedInstanceState) {  
  14.           
  15.         return inflater.inflate(R.layout.fragment_1, null);       
  16.     }     
  17. }  

        到这里整个项目就基本上讲完了,大家还可以为此项目继续完善下去,实现点击菜单选项后实现的效果以及顶部标题栏的实现。对于这个项目,博主还有一个地方不明白,就是针对此项目,在弹出菜单后,有什么好的简单一点的方法将背景置为灰色?我也试了一些方法,但是效果都不是特别的理想,希望读者有什么想法可以给博主留言,大家可以互相交流学习一下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值