Android贴吧系统学习-----悬浮布局的做法

2019-07-08 15:33:54 浏览数 (1)

代码语言:javascript复制
<span style="font-size:18px;"><strong>
	//悬浮窗口
	private LinearLayout mFloatLayout;  
    private WindowManager.LayoutParams wmParams;  
    //创建浮动窗口设置布局参数的对象  
    private WindowManager mWindowManager; 
    private boolean isFloatAttached = false;
	private int tabPosition = 0;
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(tabPosition == 2 && isFloatAttached == false)
			createFloatView();
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(null != mFloatLayout && isFloatAttached == true)
		{
			mWindowManager.removeView(mFloatLayout);
			isFloatAttached = false;
		}
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("position", tabPosition);
		outState.putBoolean("is_attached", isFloatAttached);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		tabPosition = savedInstanceState.getInt("position");
		isFloatAttached = savedInstanceState.getBoolean("is_attached");
	}
	
	switch (checkedIndex) {
		case 0:
			////////////
			tabPosition = 0;
		    if(null != mFloatLayout && isFloatAttached == true)
    		{
    			mWindowManager.removeView(mFloatLayout);
    			isFloatAttached = false;
    		}
			/////////////////
			fragment = fragments.get(0);//对应编号为0的fragment
			transaction.replace(R.id.main_framelayout, fragment);
			transaction.commit();
			title.setText("菜单1");
			break;
		case 1:
			tabPosition = 1;
		    if(null != mFloatLayout && isFloatAttached == true)
    		{
    			mWindowManager.removeView(mFloatLayout);
    			isFloatAttached = false;
    		}
			fragment = fragments.get(1);////对应编号为1的fragment
			transaction.replace(R.id.main_framelayout, fragment);
			transaction.commit();
			title.setText("菜单2");
			break;
		case 2:
			tabPosition = 2;			
		    if(isFloatAttached == false)
    			createFloatView();
			//这里放置的是发布
			break;
		case 3:
			tabPosition = 3;
		    if(null != mFloatLayout && isFloatAttached == true)
    		{
    			mWindowManager.removeView(mFloatLayout);
    			isFloatAttached = false;
    		}
			fragment = fragments.get(2);//对应编号为2的fragment
			transaction.replace(R.id.main_framelayout, fragment);
			transaction.commit();
			title.setText("菜单4");
			break;
		case 4:
			tabPosition = 4;
		    if(null != mFloatLayout && isFloatAttached == true)
    		{
    			mWindowManager.removeView(mFloatLayout);
    			isFloatAttached = false;
    		}
			fragment = fragments.get(3);//对应编号为3的fragment
			transaction.replace(R.id.main_framelayout, fragment);
			transaction.commit();
			title.setText("菜单5");
			break;

		default:
			break;
	}
	
	
	//添加浮动窗口
	 private void createFloatView()  
	    {  
	        wmParams = new WindowManager.LayoutParams();  
	        //获取的是WindowManagerImpl.CompatModeWrapper  
	        mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);  
	        //设置window type  
	        wmParams.type = LayoutParams.TYPE_PHONE;   
	        //设置图片格式,效果为背景透明  
	        wmParams.format = PixelFormat.RGBA_8888;   
	        //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)  
	        wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;        
	        //调整悬浮窗显示的停靠位置为左侧置顶  
	        wmParams.gravity = Gravity.RIGHT| Gravity.BOTTOM;         
	        // 以屏幕左上角为原点,设置x、y初始值,相对于gravity  
	        wmParams.x = Dp2Px(getApplicationContext(), 20);  
	        wmParams.y = Dp2Px(getApplicationContext(), 80);  
	  
	        //设置悬浮窗口长宽数据    
	        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
	        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
	        
	  
	         /*// 设置悬浮窗口长宽数据 
	        wmParams.width = 200; 
	        wmParams.height = 80;*/  
	        
	        LayoutInflater inflater = LayoutInflater.from(getApplication());  
	        //获取浮动窗口视图所在布局  
	        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.tieba_add_float, null); 
	        
	        //添加mFloatLayout  
	        mWindowManager.addView(mFloatLayout, wmParams);
	        isFloatAttached = true;
	        //浮动窗口按钮  
	          
	        mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,  
	                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec  
	                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));  
	        //设置监听浮动窗口的触摸移动  
	        ImageView addImg = (ImageView)mFloatLayout.findViewById(R.id.add_float);
	        addImg.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					startActivity(new Intent(NewStartActivity.this,WritePostActivity.class));
				}
			});
	    } 
	</strong></span>

在上面我的菜单用的是   radioGruop控件,只需要在自己需要的界面中把悬浮布局弄出来就行,

另外在onstop 和onresume生命周期里面也需要控制下,

0 人点赞