Android开发随手记--系统悬浮框

最近同事装了个天气通,发现它的晴天特效很炫啊,而且在系统界面上也会出现,觉得很不错,特研究了下,其实很简单,就是改下WindowMananger的属性就可以了,然后用WindowManager添加要显示的界面就OK了,代码如下:

public class PopupActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		
		Button test = new Button(this);
		test.setText("Test");
		WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		WindowManager.LayoutParams lp = new LayoutParams();
                //关键属性
		lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
 		lp.format = 1;
		lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		lp.flags = lp.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		lp.flags = lp.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
		lp.width = 140;
		lp.height = 140;
		wm.addView(test, lp);
	}

}

同时别忘了在AndroidManifest.xml里添加权限:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>


发现很多比较不错的功能,实现其实并不复杂,关键在对每个接口的了解程度


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值