[Android] 底部菜单布局+PopupWindows实现弹出菜单功能(初级篇)

2023-11-03

    这篇文章主要是自己研究如何对底部菜单进行布局,并简单的实现点击不同"按钮"实现图片切换和背景切换的功能,最后通过PopupWindows实现弹出菜单,点击不同按钮能实现不同方法,相当于美图秀秀编辑图片的功能吧!它并没有涉及到Fragment碎片切换页面的功能,因为页面始终显示被处理的图片.这是我初学Android的一篇基础性文章和在线思想笔记,网上有很多更优秀的demo,不过也希望对大家有用~
    首先介绍两种方法实现底部菜单点击不同图标显示选中状态的效果.
    (可参考简短文章:Android_UI_点击按钮切换背景效果实现)

    一. 底部菜单 第一种方法

    它显示的效果如下图所示,其中底部菜单布局采用多个LinearLayout进行,点击不同"按钮"可以改变其背景图片.

              

    首先介绍它的activity_main.xml布局:
    1.它采用3个RelativeLayout相对布局进行,分别对应标题路径、中间显示图片和底部的菜单栏;
    2.底部菜单栏由5个LinearLayout水平布局组成,每一个LinearLayout都由ImageView和TextView组成.
    代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.touchimagetest.MainActivity"
    tools:ignore="MergeRootFrame" >
     <!-- 顶部路径 -->
    <RelativeLayout 
        android:id="@+id/MyLayout_top"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="40dp" 
        android:layout_alignParentTop="true"
        android:gravity="center">
        <TextView
	        android:id="@+id/textView1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textSize="15sp"
	        android:text="显示图片操作" />
    </RelativeLayout>
    <!-- 底部按钮 -->
    <RelativeLayout
        android:id="@+id/MyLayout_bottom"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/image_toolbar_bg"
        android:gravity="center">
	    <LinearLayout 
	        android:layout_width="match_parent"
	    	android:layout_height="match_parent"
	    	android:orientation="horizontal"
	    	android:layout_alignParentBottom="true" >
	       
	        <LinearLayout 
                android:id="@+id/layout_watch"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"
                android:gravity="center"  
    			android:orientation="vertical" >  
                <ImageView  
                    android:id="@+id/image_watch"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                   	android:padding="3dp" 
                    android:src="@drawable/tab_watch_btn" />  
                <TextView  
                    android:id="@+id/textview_watch"
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"   
                    android:text="查看"  
                    android:textColor="#ffffff"  
                    android:textSize="10sp" />  
            </LinearLayout>  
            
	        <LinearLayout 
                android:id="@+id/layout_increase"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"
                android:gravity="center"  
    			android:orientation="vertical" >  
                <ImageView  
                    android:id="@+id/image_increase"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                   	android:padding="3dp" 
                    android:src="@drawable/tab_increase_btn" />  
                <TextView  
                    android:id="@+id/textview_increase"
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"   
                    android:text="增强"  
                    android:textColor="#ffffff"  
                    android:textSize="10sp" />  
            </LinearLayout>  
            
	        <LinearLayout 
                android:id="@+id/layout_effect"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"
                android:gravity="center"  
    			android:orientation="vertical" >  
                <ImageView  
                    android:id="@+id/image_effect"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                   	android:padding="3dp" 
                    android:src="@drawable/tab_effect_btn" />  
                <TextView  
                    android:id="@+id/textview_effect"
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"   
                    android:text="特效"  
                    android:textColor="#ffffff"  
                    android:textSize="10sp" />  
            </LinearLayout>
            
	        <LinearLayout 
                android:id="@+id/layout_frame"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"
                android:gravity="center"  
    			android:orientation="vertical" >  
                <ImageView  
                    android:id="@+id/image_frame"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                   	android:padding="3dp" 
                    android:src="@drawable/tab_frame_btn" />  
                <TextView  
                    android:id="@+id/textview_frame"
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"   
                    android:text="相框"  
                    android:textColor="#ffffff"  
                    android:textSize="10sp" />  
            </LinearLayout> 
            
	        <LinearLayout 
                android:id="@+id/layout_person"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"
                android:gravity="center"  
    			android:orientation="vertical" >  
                <ImageView  
                    android:id="@+id/image_person"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                   	android:padding="3dp" 
                    android:src="@drawable/tab_person_btn" />  
                <TextView  
                    android:id="@+id/textview_person"
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"   
                    android:text="美白"  
                    android:textColor="#ffffff"  
                    android:textSize="10sp" />  
            </LinearLayout>
            
 		</LinearLayout>  		
	</RelativeLayout>
	<!-- 显示图片 -->
    <RelativeLayout
        android:id="@+id/Content_Layout" 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_above="@id/MyLayout_bottom" 
        android:layout_below="@id/MyLayout_top"
        android:background="#EFDFDF"
        android:gravity="center">
        <ImageView
	        android:id="@+id/imageView1"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:layout_gravity="center_horizontal"
			android:scaleType="matrix" />
    </RelativeLayout>
</RelativeLayout>
    此时你需要注意的是为每个LinearLayout中ImageView指定src时并不是原图片,而是drawable中的xml文件,如<ImageView android:id="@+id/image_watch".. />.

 
    同时每个drawable中的xml文件对应每个相应的按钮,上图中effect(效果)、increase(增强)、frame(相框)、watch(查看)、person(美白).其中每个格式基本如下,如tab_watch_btn.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/image_icon_watch_sel" android:state_selected="true"/>
    <item android:drawable="@drawable/image_icon_watch_sel" android:state_pressed="true"/> 
    <item android:drawable="@drawable/image_icon_watch_nor"/> 
</selector>
    其中state_selected表示"选中"状态,state_pressed表示"点击"效果,而最后的<item android:drawable="@drawable/image_icon_watch_nor"/> 表示默认情况显示图片,原图片如下:

    同时设置选中状态"按钮"的背景加黑效果,在drawable中添加selector_tab_background.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
	<item android:drawable="@drawable/image_home_layout_bg" android:state_pressed="true"/>  
    <item android:drawable="@drawable/image_home_layout_bg" android:state_selected="true"/>  
</selector>

    然后在MainActivity.java中添加自定义变量,主要是LinearLayout(点击它响应事件)和ImageView(切换图标).

//自定义变量
private LinearLayout layoutWatch;             //查看图片
private LinearLayout layoutIncrease;           //增强图片
private LinearLayout layoutEffect;               //图片特效
private LinearLayout layoutFrame;              //图片边框
private LinearLayout layoutPerson;             //图片美白

private ImageView imageWatch;    
private ImageView imageIncrease;  
private ImageView imageEffect;  
private ImageView imageFrame;  
private ImageView imagePerson; 

    然后添加代码如下,该种方法需要在点击按钮中设置其他LinearLayout图标状态为未选择状态,否则会出现点击按钮的效果(即:点击就切换图标一次,我们需要的是点击就状态长显).

//创建活动
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //布局
    layoutWatch = (LinearLayout) findViewById(R.id.layout_watch);
    layoutIncrease = (LinearLayout) findViewById(R.id.layout_increase);
    layoutEffect = (LinearLayout) findViewById(R.id.layout_effect);
    layoutFrame = (LinearLayout) findViewById(R.id.layout_frame);
    layoutPerson = (LinearLayout) findViewById(R.id.layout_person);
    //图标 
    imageWatch = (ImageView) findViewById(R.id.image_watch);
    imageIncrease = (ImageView) findViewById(R.id.image_increase);
    imageEffect = (ImageView) findViewById(R.id.image_effect);
    imageFrame = (ImageView) findViewById(R.id.image_frame);
    imagePerson = (ImageView) findViewById(R.id.image_person);
    //初始化布局
    initView();

    //按钮一 监听事件 查看图片
    layoutWatch.setOnClickListener( new OnClickListener() {
    	@Override
    	public void onClick(View v) {
            //设置背景图片加深
    		Toast.makeText(MainActivity.this, "点击按钮1", Toast.LENGTH_SHORT).show();
    		layoutWatch.setBackgroundResource(R.drawable.selector_tab_background);
    		//设置图标选中情况
    		layoutWatch.setSelected(true);
    		layoutIncrease.setSelected(false);
    		layoutEffect.setSelected(false);
    		layoutFrame.setSelected(false);
    		layoutPerson.setSelected(false);
    	}
    });
    //按钮二 监听事件增强图片
    layoutIncrease = (LinearLayout) findViewById(R.id.layout_increase);
    layoutIncrease.setOnClickListener( new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		layoutIncrease.setBackgroundResource(R.drawable.selector_tab_background);
    		//设置图标选中情况
    		layoutWatch.setSelected(false);
    		layoutIncrease.setSelected(true);
    		layoutEffect.setSelected(false);
    		layoutFrame.setSelected(false);
    		layoutPerson.setSelected(false);
    	}
    });
    //按钮三 监听事件图片特效
    layoutEffect = (LinearLayout) findViewById(R.id.layout_effect);
    layoutEffect.setOnClickListener( new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		//设置背景图片
    		layoutEffect.setBackgroundResource(R.drawable.selector_tab_background);
    		//设置图标选中情况
    		layoutWatch.setSelected(false);
    		layoutIncrease.setSelected(false);
    		layoutEffect.setSelected(true);
    		layoutFrame.setSelected(false);
    		layoutPerson.setSelected(false);
    	}
    });
    //按钮四 监听事件图片相框
    layoutFrame = (LinearLayout) findViewById(R.id.layout_frame);
    layoutFrame.setOnClickListener( new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		//设置背景图片
    		layoutFrame.setBackgroundResource(R.drawable.selector_tab_background);
    		//设置图标选中情况
    		layoutWatch.setSelected(false);
    		layoutIncrease.setSelected(false);
    		layoutEffect.setSelected(false);
    		layoutFrame.setSelected(true);
    		layoutPerson.setSelected(false);

    	}
    });
  	//按钮五 监听事件图片美白
    layoutPerson = (LinearLayout) findViewById(R.id.layout_person);
    layoutPerson.setOnClickListener( new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		//设置背景图片
    		layoutPerson.setBackgroundResource(R.drawable.selector_tab_background);
    		//设置图标选中情况
    		layoutWatch.setSelected(false);
    		layoutIncrease.setSelected(false);
    		layoutEffect.setSelected(false);
    		layoutFrame.setSelected(false);
    		layoutPerson.setSelected(true);

    	}
    });
}

//初始化布局
private void initView()
{
	imageWatch.setImageResource(R.drawable.tab_watch_btn);
	imageIncrease.setImageResource(R.drawable.tab_increase_btn);
	imageEffect.setImageResource(R.drawable.tab_effect_btn);
	imageFrame.setImageResource(R.drawable.tab_frame_btn);
	imagePerson.setImageResource(R.drawable.tab_person_btn);
}

    二. 底部菜单 第二种方法

   其中activity_main.xml中布局与第一个相同,不同的是在xml中就指定drawable-hdpi中原图片名,因为它不在调用drawable如tab_watch_btn.xml文件,而使用代码直接操作.其中5个LinearLayout一个如下所示:
<LinearLayout 
         android:id="@+id/layout_watch"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"
         android:gravity="center"  
		 android:orientation="vertical" >  
         <ImageView  
             android:id="@+id/image_watch"  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
            	android:padding="3dp" 
             android:src="@drawable/image_icon_watch_nor" />  
         <TextView  
             android:id="@+id/textview_watch"
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"   
             android:text="查看"  
             android:textColor="#ffffff"  
             android:textSize="10sp" />  
</LinearLayout>  
   此时它的文件夹结构如下图,drawble没有设置背景加深和加载图标的xml文件:
    同时5个LinearLayout(查看、增强、特效、相框、美白)设置触屏响应事件:
layoutWatch.setOnTouchListener(new OnTouchListener() {
	@Override
    public boolean onTouch(View v, MotionEvent event) {               
        if(event.getAction() == MotionEvent.ACTION_DOWN) {       
            //按下背景图片             	
        	layoutWatch.setBackgroundResource(R.drawable.image_home_layout_bg);
        	layoutIncrease.setBackgroundResource(R.drawable.image_home_layout_no);
        	layoutEffect.setBackgroundResource(R.drawable.image_home_layout_no);
        	layoutFrame.setBackgroundResource(R.drawable.image_home_layout_no);
        	layoutPerson.setBackgroundResource(R.drawable.image_home_layout_no);
        	//设置按钮图片
        	imageWatch.setImageDrawable(getResources().getDrawable(R.drawable.image_icon_watch_sel));    
        	imageIncrease.setImageDrawable(getResources().getDrawable(R.drawable.image_icon_increase_nor)); 
        	imageEffect.setImageDrawable(getResources().getDrawable(R.drawable.image_icon_effect_nor)); 
        	imageFrame.setImageDrawable(getResources().getDrawable(R.drawable.image_icon_frame_nor)); 
        	imagePerson.setImageDrawable(getResources().getDrawable(R.drawable.image_icon_person_nor)); 
        }
        return false;       
	}       
});  
   需要注意的是网上下面这段代码仅实现点击一下图片变换的效果,而如果想要实现长显需要如我上面的所示.还见到一个使用Radio实现该效果,当点击一次时判断是否选中并显示相应图片.而使用FragmentTabHost实现同样效果,我不知其原理~
bottomReturnBtn.setOnTouchListener(new OnTouchListener() {  
      //这段代码仅仅实现点击一次改变图标功能
    public boolean onTouch(View v, MotionEvent event) {  
        Button upStepBtn = (Button) v;  
        if(event.getAction() == MotionEvent.ACTION_DOWN){  
            upStepBtn.setBackgroundResource(R.drawable.bottom_sub_order_btn);  
        }else if(event.getAction() == MotionEvent.ACTION_UP){  
            upStepBtn.setBackgroundResource(R.drawable.bottom_return_check);  
            finish();   
        }  
        return false;  
    }  
});   

    三. PopupWindow实现弹出菜单

    然后讲解如何通过PopupWindow实现下面的功能.效果如下图所示,简单实现PopupWindow功能可结合下面两篇文章:
    android之popupWindow在指定位置上的显示
    [android开发] 使用PopupWindow实现页面点击顶部弹出下拉菜单
    首先,为PopupWindow设置动画效果,在res文件夹下添加文件夹anim,然后添加anim_entry.xml文件:
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">    
    <translate    
        android:fromXDelta="0"    
        android:toXDelta="0"    
        android:fromYDelta="120"    
        android:toYDelta="0"    
        android:duration="500" />    
</set>
   它是出现效果:从菜单栏底部向上弹出,同时添加anim_exit.xml:
<?xml version="1.0" encoding="UTF-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
    <!-- 透明度消失 -->
    <alpha 
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" 
        android:duration="200" />
</set>
   最后在res/values文件夹styles.xml中添加动画效果,通过调用name="AnimationPreview"可以实现动画:
<!-- 定义popupwindows动画效果 -->
 <style name="AnimationPreview">  
     <item name="android:windowEnterAnimation">@anim/anim_entry</item>  
     <item name="android:windowExitAnimation">@anim/anim_exit</item>  
 </style>  
   然后你需要自定义弹出PopupWindow的布局xml文件,如popup_effect.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" >  
  
    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:background="@drawable/image_button_bg_left"  
        android:orientation="vertical" >  
        <LinearLayout  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_horizontal"  
            android:layout_marginTop="5dp"  
            android:orientation="horizontal" > 
             
            <LinearLayout  
                android:id="@+id/layout_effect_hj"
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_margin="2dp"  
                android:layout_weight="1"  
                android:orientation="vertical" >  
                <ImageView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:src="@drawable/image_effect_hj" />
                <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:shadowColor="#ff000000"  
                    android:shadowDx="1.0"  
                    android:shadowDy="1.0"  
                    android:shadowRadius="1.0"  
                    android:text="怀旧"  
                    android:textColor="#ffffffff"  
                    android:textSize="13.0dip" />  
            </LinearLayout>  
  
            <LinearLayout  
                android:id="@+id/layout_effect_fd"
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_margin="2dp"  
                android:layout_weight="1"  
                android:orientation="vertical" >  
                <ImageView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:src="@drawable/image_effect_fd" />  
                <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:shadowColor="#ff000000"  
                    android:shadowDx="1.0"  
                    android:shadowDy="1.0"  
                    android:shadowRadius="1.0"  
                    android:text="浮雕"  
                    android:textColor="#ffffffff"  
                    android:textSize="13.0dip" />  
            </LinearLayout>  

            <LinearLayout  
                android:id="@+id/layout_effect_gz"
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_margin="2dp"  
                android:layout_weight="1"  
                android:orientation="vertical" >  
                <ImageView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:src="@drawable/image_effect_gz" /> 
                <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:shadowColor="#ff000000"  
                    android:shadowDx="1.0"  
                    android:shadowDy="1.0"  
                    android:shadowRadius="1.0"  
                    android:text="光照"  
                    android:textColor="#ffffffff"  
                    android:textSize="13.0dip" />  
            </LinearLayout>  
  
            <LinearLayout  
                android:id="@+id/layout_effect_sm"
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_margin="2dp"  
                android:layout_weight="1"  
                android:orientation="vertical" >  
                <ImageView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:src="@drawable/image_effect_sm" />  
                <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:shadowColor="#ff000000"  
                    android:shadowDx="1.0"  
                    android:shadowDy="1.0"  
                    android:shadowRadius="1.0"  
                    android:text="素描"  
                    android:textColor="#ffffffff"  
                    android:textSize="13.0dip" />  
            </LinearLayout>  
  
            <LinearLayout  
                android:id="@+id/layout_effect"
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_margin="2dp"  
                android:layout_weight="1"  
                android:orientation="vertical" >  
                <ImageView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:src="@drawable/image_effect" />  
                <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:layout_marginTop="1.0dip"  
                    android:shadowColor="#ff000000"  
                    android:shadowDx="1.0"  
                    android:shadowDy="1.0"  
                    android:shadowRadius="1.0"  
                    android:text="锐化"  
                    android:textColor="#ffffffff"  
                    android:textSize="13.0dip" />  
            </LinearLayout>  
            
        </LinearLayout>  
    </LinearLayout>  
</RelativeLayout>
   它的在Xml中Graphical Layout显示效果如下图所示:
             
    添加5个自定义变量:
//弹出按钮
private PopupWindow popupWindow1;     
private PopupWindow popupWindow2;
private PopupWindow popupWindow3;
private PopupWindow popupWindow4;
private PopupWindow popupWindow5;
   然后当点击"相框"布局LinearLayout时,添加代码如下(其他类似):
//按钮四 监听事件图片相框
layoutFrame.setOnClickListener( new OnClickListener() {
	@Override
	public void onClick(View v) {
		//载入PopupWindow
		if (popupWindow4 != null&&popupWindow4.isShowing()) {  
            popupWindow4.dismiss();  
            return;  
        } else {  
            initmPopupWindowView(4);   //number=4
            int[] location = new int[2];  
            v.getLocationOnScreen(location); 
            Toast.makeText(MainActivity.this, ""+location[0], Toast.LENGTH_SHORT).show();
            popupWindow4.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow4.getHeight());
        }
	}
});
   其中initmPopupWindowView(int number)为自定义函数,参数对应的是点击LinearLayout的序号,点击"按钮"4即传入数字4:
public void initmPopupWindowView(int number) {  
    	View customView = null;
        // 获取自定义布局文件  
    	if(number==1) { //查看
    		customView = getLayoutInflater().inflate(R.layout.popup_watch, null, false);  
    		// 创建PopupWindow实例  (250,180)分别是宽度和高度  
            popupWindow1 = new PopupWindow(customView, 250, 280); 
            // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]  
            popupWindow1.setAnimationStyle(R.style.AnimationPreview);  
            // 自定义view添加触摸事件  
            customView.setOnTouchListener(new OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    if (popupWindow1 != null && popupWindow1.isShowing()) {  
                        popupWindow1.dismiss();  
                        popupWindow1 = null;  
                    }  
                    return false;  
                }  
            });  
    	}
    	 if(number==2) { //增强
         	customView = getLayoutInflater().inflate(R.layout.popup_increase, null, false);  
         	popupWindow2 = new PopupWindow(customView, 450, 150);
         	// 使其聚集 要想监听菜单里控件的事件就必须要调用此方法   
             popupWindow2.setFocusable(true);  
             // 设置允许在外点击消失   
             popupWindow2.setOutsideTouchable(true); 
             popupWindow2.setAnimationStyle(R.style.AnimationPreview);  
             // 自定义view添加触摸事件  
             customView.setOnTouchListener(new OnTouchListener() {  
                 @Override  
                 public boolean onTouch(View v, MotionEvent event) {  
                     if (popupWindow2 != null && popupWindow2.isShowing()) {  
                         popupWindow2.dismiss();  
                         popupWindow2 = null;  
                     }  
                     return false;  
                 }  
             });  
         }
        if(number==3) { //效果
        	customView = getLayoutInflater().inflate(R.layout.popup_effect, null, false);  
        	popupWindow3 = new PopupWindow(customView, 450, 150);
        	// 使其聚集 要想监听菜单里控件的事件就必须要调用此方法   
            popupWindow3.setFocusable(true);  
            // 设置允许在外点击消失   
            popupWindow3.setOutsideTouchable(true); 
            popupWindow3.setAnimationStyle(R.style.AnimationPreview);  
            // 自定义view添加触摸事件  
            customView.setOnTouchListener(new OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    if (popupWindow3 != null && popupWindow3.isShowing()) {  
                        popupWindow3.dismiss();  
                        popupWindow3 = null;  
                    }  
                    return false;  
                }  
            });  
        }
        if(number==4) {
        	customView = getLayoutInflater().inflate(R.layout.popup_frame, null, false);  
        	popupWindow4 = new PopupWindow(customView, 450, 150);
        	// 使其聚集 要想监听菜单里控件的事件就必须要调用此方法   
            popupWindow4.setFocusable(true);  
            popupWindow4.setAnimationStyle(R.style.AnimationPreview);  
            // 自定义view添加触摸事件  
            customView.setOnTouchListener(new OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    if (popupWindow4 != null && popupWindow4.isShowing()) {  
                        popupWindow4.dismiss();  
                        popupWindow4 = null;  
                    }  
                    return false;  
                }  
            });  
        }
        if(number==5) {
        	customView = getLayoutInflater().inflate(R.layout.popup_frame, null, false);  
        	popupWindow5 = new PopupWindow(customView, 450, 150);
        	// 使其聚集 要想监听菜单里控件的事件就必须要调用此方法   
            popupWindow5.setFocusable(true);  
            popupWindow5.setAnimationStyle(R.style.AnimationPreview);  
            // 自定义view添加触摸事件  
            customView.setOnTouchListener(new OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    if (popupWindow5 != null && popupWindow5.isShowing()) {  
                        popupWindow5.dismiss();  
                        popupWindow5 = null;  
                    }  
                    return false;  
                }  
            });  
        } //end if
    }

    四. PopupWindow实现点击效果

    做到这里,你就能实现点击底部菜单实现弹出PopupWindows效果,但显然是不足的.怎样通过点击弹出PopupWindow中的按钮实现做不同的事情呢?下面讲解,你只需要添加下面的代码即可实现"特效"效果.
    代码如下,你可以自定义函数实现不同效果功能(结合前面几篇文章,我的美图秀秀基本完成):
if(number==3) { //效果
	customView = getLayoutInflater().inflate(R.layout.popup_effect, null, false);  
	popupWindow3 = new PopupWindow(customView, 450, 150);
	// 使其聚集 要想监听菜单里控件的事件就必须要调用此方法   
    popupWindow3.setFocusable(true);  
    // 设置允许在外点击消失   
    popupWindow3.setOutsideTouchable(true); 
    popupWindow3.setAnimationStyle(R.style.AnimationPreview);  
    // 自定义view添加触摸事件  
    customView.setOnTouchListener(new OnTouchListener() {  
        @Override  
        public boolean onTouch(View v, MotionEvent event) {  
            if (popupWindow3 != null && popupWindow3.isShowing()) {  
                popupWindow3.dismiss();  
                popupWindow3 = null;  
            }  
            return false;  
        }  
    });  
    //判断点击子菜单不同按钮实现不同功能
    LinearLayout layoutEffect1 = (LinearLayout) customView.findViewById(R.id.layout_effect_hj);
    layoutEffect1.setOnClickListener(new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(MainActivity.this, "效果-怀旧", Toast.LENGTH_SHORT).show();
    	}
    });
    LinearLayout layoutEffect2 = (LinearLayout) customView.findViewById(R.id.layout_effect_fd);
    layoutEffect2.setOnClickListener(new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(MainActivity.this, "效果-浮雕", Toast.LENGTH_SHORT).show();
    	}
    });
    LinearLayout layoutEffect3 = (LinearLayout) customView.findViewById(R.id.layout_effect_gz);
    layoutEffect3.setOnClickListener(new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(MainActivity.this, "效果-光照", Toast.LENGTH_SHORT).show();
    	}
    });
    LinearLayout layoutEffect4 = (LinearLayout) customView.findViewById(R.id.layout_effect_sm);
    layoutEffect4.setOnClickListener(new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(MainActivity.this, "效果-素描", Toast.LENGTH_SHORT).show();
    		//打开图片
    		OpenImage();
    	}
    });
    LinearLayout layoutEffect5 = (LinearLayout) customView.findViewById(R.id.layout_effect);
    layoutEffect5.setOnClickListener(new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		Toast.makeText(MainActivity.this, "效果-锐化", Toast.LENGTH_SHORT).show();
    	}
    });
}

    五. 总结

    本文章主要讲述如何加载菜单栏在底部,同时讲述PopupWindows弹出事件,其实更好的布局方法是通过适配器,但是我才学Android,很多东西还不懂.所以它只是一篇初级文章,但完全能实现需要功能.
    适配器参考:
Android之用PopupWindow实现弹出菜单
    最后希望文章对大家有所帮助,如果文章中有错误或不足之处见谅~
    上联:只有真正的做了,才知道自己的技术有多渣
    下联:只有真正做完了,才知道自己的成就有多高
    横批:同志仍需努力
                                  ——By:Eastmount
    下载地址demo:http://download.csdn.net/detail/eastmount/8139907
    (By:Eastmount 2014-11-6 中午12点
http://blog.csdn.net/eastmount/)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[Android] 底部菜单布局+PopupWindows实现弹出菜单功能(初级篇) 的相关文章

  • animation.start() 或animation.startNow() 不会立即开始动画

    我有一个奇怪的问题 有时应该淡出我的控件 ImageButton 的动画不会立即启动 我使用淡出动画来隐藏它 然后在 myListener 的末尾 onAnimationEnd 中 我将新资源作为按钮上的图像 我的应用程序代码中的某处 An
  • 具有自定义厚度的虚线分隔符

    我有一个虚线分隔符
  • Android 位置 API:获取提供商状态

    在 Android 中获取位置提供商状态的最佳方式是什么 当注册 LocationListener 以进行位置更新时 onStatusChanged String provider int status Bundle extras 每当状态
  • Android:将图像裁剪为特定尺寸

    我的目的是让用户从图库中选择一张图像 然后进行裁剪活动 但是 我需要将定义裁剪蒙版的矩形锁定到某个尺寸 然后用户只需重新定位它即可显示图像的一部分 关于如何做到这一点有什么想法吗 Thanks T Intent intent new Int
  • Android 中是否可以使用滑动视图导航的刻度线?

    我想在 Android 应用程序中创建一组页面 这些页面可以水平滑动并使用刻度线来指示当前页面在我拥有的 12 个页面集中的位置 Android 设计文档中提到了像这样使用刻度线的设计模式 http developer android co
  • adb 今天无法连接到 VirtualBox 中的 Android-x86

    我在 VirtualBox 中成功安装了 Android x86 v3 2 eeepc 一段时间 这是帮助我在 Android 上调试 USB 主机应用程序的好方法 此类配置的安装是 安装VirtualBox 将 Android x86 v
  • 获取Android库中的上下文

    我正在编写一个 Android 应用程序 它的一些功能封装在内部库中 但是 要使此功能发挥作用 库需要一个应用程序上下文的实例 为图书馆提供这种上下文的最佳方式是什么 我看到了一些选择 但没有一个有吸引力 Have my library c
  • 返回到上一个活动的最后状态

    我有两个活动 A 和 B 当应用程序启动时 活动 A 会被加载 我会从数据库将数据加载到其中 我可以毫无问题地转到活动 B 但是当我返回活动 A 时 会查看旧数据一段时间 然后重新加载活动 知道如何解决这个问题吗 在从数据库加载数据之前清除
  • Ionic 框架 - Config.xml

    我需要修改 config xml 文件 因此在针对 Android 进行编译时我会获取以下权限
  • webview 中的 android 移动 Twitter 页面无法打开?

    我正在尝试打开以 https 开头的 Twitter 页面webview在安卓中 但它没有打开 并且持续加载时出现黑屏 屏幕截图如下 Edit 在主网络视图中我有 Twitter 页面的链接https twitter com mytwitt
  • 绘制平滑曲线

    我想创建更平滑的曲线 而不仅仅是线角 这是我现在画的图 这是我的代码 case FREEHAND float pts float ptk ptk new float 2 imageMatrix invert inv if mCurrentS
  • 作为 ViewPager 的一部分更新 ListFragment 中的数据

    我在 Android 中使用 v4 兼容性 ViewPager 我的 FragmentActivity 有一堆数据 这些数据将以不同的方式显示在我的 ViewPager 的不同页面上 到目前为止 我只有同一个 ListFragment 的
  • Firebase 身份验证中是否需要 SMS Retriever API 才能进行 otp 自动填充?

    我在用Firebase auth api验证用户电话号码 目前 就我而言 我有一个屏幕 用户在其中输入电话号码 并在第二个屏幕上输入密码 根据文档firebase大多数时候自动检索 OTP 并启动验证过程 所以我的问题是它已经实现了SMS
  • 错误膨胀类 android.support.design.widget.NavigationView [启动时崩溃]

    该应用程序应该有一个导航抽屉 可以从左侧拉出并显示各种活动 但是一旦将导航栏添加到 XML Activity homescreen 文档中 应用程序一启动就会崩溃 主屏幕 java package com t99sdevelopment c
  • Android - 在图像/缩略图上覆盖播放按钮的最佳方式

    我有一个 Android 应用程序 可以播放音频 视频并显示图片 对于视频 我想在预览图像顶部以及列表视图中叠加一个播放按钮 现在我的做法是使用 xml 中的 ImageView 然后可绘制对象是一个图层图层列表 我以编程方式定义它 因为其
  • java.lang.UnsatisfiedLinkError - android studio gradle 中的 NDK?

    文件夹结构 app main java jni Android mk Application mk hello jni c res 在构建 gradle apply plugin com android application androi
  • Android Studio - 无法解析符号“firebase”

    我目前正在将应用程序升级到新的 Firebase 版本 我按照指南进行操作 包括classpath com google gms google services 3 0 0 在我的项目 build gradle 的依赖项中以及compile
  • 无法使用 Zxing Android 扫描 Code 128

    我已将 ZXing 条码阅读器集成到我的 Android 应用程序中 它可以扫描大部分代码 但我有一张电费单 它无法扫描 使用 Galaxy Ace 5 0MP 我认为问题是由于相机分辨率和焦点造成的 因为当我使用 Xperia SL 12
  • Recyclerview项目点击涟漪效果[重复]

    这个问题在这里已经有答案了 我正在尝试添加Ripple影响到RecyclerView的项目 我在网上查了一下 但找不到我需要的东西 我努力了android background归因于RecyclerView本身并将其设置为 android
  • PhoneGap Build Android 不显示闪屏

    这是我的 config xml 中与启动屏幕相关的代码

随机推荐

  • linux命令整理

    pwd 在两个不同目录下使用此命令 ls 长格式 短格式 多个短格式组合使用 cd 上一级目录 下一级目录 相对路径使用 绝对路径使用 特殊符号使用 less more 注意找区别 less的作用与more十分相似 不同点为less命令允许
  • linux下生成静态库.a与动态库.o的方法流程

    比如我们随便从github下载一个zlib的开源库 作者都是告诉了我们这个库的使用方法 并写好了MakeLists txt了 所以直接 先建立一个build文件夹 用来存放生成的编译文件 gt 接着cmake gt 再make 编译后得到了
  • vmware VSAN 双节点部署

    一 环境 ESxi主机 01 192 168 2 26 ESxi主机 02 192 168 2 27 见证主机 虚拟机 192 168 2 157 部署在ESxi 主机01上 注 此操作官方不推荐 192 168 2 26虚拟交换机配置如下
  • Python中default()函数

    阅读textual inversion代码的时候 遇到这样一个用法 def p losses self x start t noise None noise default noise lambda torch randn like x s
  • 【python】运行/调用/执行/终止/重启.exe文件

    一 运行 调用 执行 打开 exe文件 1 os system cmd command 使用 import os os system cmd command 括号里输入的参数即为在cmd里面输入的内容 具体格式参照这个连接 python中o
  • UE5实现距离测量功能

    文章目录 1 实现目标 2 实现过程 2 1 Widget 2 2 蓝图实现 3 参考资料 1 实现目标 UE5在Runtime环境下测量两个空间点位之间的绝对距离 并支持多段线的距离测量 GIF动图如下所示 2 实现过程 实现原理比较简单
  • Django新增自定义模板函数

    Django新增自定义模板函数 1 创建templatetags文件 2 创建一个 py文件 coding utf 8 from django import template register名称不可改 register template
  • 【数据结构与算法】3、虚拟头节点、动态数组的缩容、动态数组和单链表的复杂度、数组的随机访问

    目录 一 虚拟头节点 二 数组的随机访问 三 动态数组 链表复杂度分析 四 动态数组 add E element 复杂度分析 五 动态数组的缩容 一 虚拟头节点 为了让代码更加精简 统一所有节点的处理逻辑 可以在最前面增加一个虚拟的头节点
  • mybatis-plus设置主键自增 ,获取自增主键id

    第一步 实体类加注解 在主键上加 TableId type IdType AUTO 注解 第二步 在数据库设置主键自增 第二种 可以在mapper插入标签中添加keyProperty id useGeneratedKeys true
  • Courses

    点击打开链接 Problem Description Consider a group of N students and P courses Each student visits zero one or more than one co
  • 网络编程-----socket函数

    1 Socket 函数 访问底层操作系统接口的全部方法 提供服务中心类 简化网络服务器的开发 语法 socket socket family type proto family 套接字家族可以是 AF UNIX 或者 AF INET typ
  • Vue.js 2 渐进式前端框架 的最佳学习方法

    Vue js作为一个后起的前端框架 借鉴了Angular React等现代前端框架 库的诸多特点 并且 取得了相当不错的成绩 Vue js的定位是一个渐进式框架 作者的说法是 与其他框架的区别就是渐进式的想法 也就是Progressive
  • 转:Python2字符编码问题汇总

    这篇文章的部分问题在Python3以后不再存在 老猿只是觉得文章的部分内容还是有参考价值 因此在此原文转发连接 Python2字符编码问题汇总
  • 第三章——Lyapunov理论基础

    文章目录 3 1 非线性系统和平衡点 非线性系统 自治与非自治系统 平衡点 常规运动 3 2 稳定性的概念 稳定性与非稳定性 渐进稳定性和指数稳定性 局部和全局稳定性 3 3 线性化和局部稳定性 3 4 Lyapunov直接法 正定函数和L
  • python的高级变量类型

    1 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 int 浮点型 float 布尔型 bool 真 True 非 0 数 非零即真 假 False 0 复数型 complex 主要用于科学计算 例如 平面场问题 波动
  • (C语言)关于浮点数和0比较大小

    对于浮点数a 不能用if a 0 来判断a与0的大小 应该判断a是否位于0附近的一个很小的区间 EPS EPS 中 或者说a的绝对值小于等于一个很小的数EPS 可定义EPS 1e 6 即用if fabs a lt EPS 正确的实数与0的比
  • 【PHP基础知识】——操作Email

    邮件已经成为我们生活中不可或缺的信息沟通方式 时常需要我们去群发或者定时发送一下邮件给指定对象 例如系统的故障报警邮件 批量回复一些服务信息等 因此 将发送邮件功能做到后台可配置或者自动化是程序开发的重要部分 像Java等语言一样 下面我们
  • 看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-2

    Vulnhub靶机DriftingBlues 1渗透测试详解 Vulnhub靶机介绍 Vulnhub靶机下载 Vulnhub靶机漏洞详解 信息收集 暴力破解 漏洞利用 反弹shell nmap提权 获取flag Vulnhub靶机渗透总结
  • docker 端口映射错误解决方法

    COMMAND FAILED sbin iptables t nat A DOCKER p tcp d 0 0 dport 8111 j DNAT to destination 172 17 0 6 8111 i docker0 faile
  • [Android] 底部菜单布局+PopupWindows实现弹出菜单功能(初级篇)

    这篇文章主要是自己研究如何对底部菜单进行布局 并简单的实现点击不同 按钮 实现图片切换和背景切换的功能 最后通过PopupWindows实现弹出菜单 点击不同按钮能实现不同方法 相当于美图秀秀编辑图片的功能吧 它并没有涉及到Fragment