标签:des android style color io os ar java for
package com.example.desktoptxt; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager.LayoutParams; import android.view.WindowManager; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; /** * 实现桌面View * * 1)将要显示在桌面的view,通过WindowManager.addView,挂在到WindowManager下; 要注意,WindowManager对象来源,源码里解释。 * 并且配置好view的WindowManager.LayoutParams * 1-1)WindowManager.LayoutParams.type 设置为 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;时, * 桌面UI不可以直接setOnClickListener(),该View的onTouchEvent()函数也无效了 * 设置为WindowManager.LayoutParams.TYPE_SYSTEM_ALERT(2003)或者WindowManager.LayoutParams.TYPE_PHONE2002才有效。 * 1-2)WindowManager.LayoutParams.flag 设置为LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE * 如果设置了WindowManager.LayoutParams.type为2002,2003;那么flag不设置为这两个flag的话,那么其他组件就无法获得touch和focus事件了。 * 设置了该两个属性之后就大家都可以获得Touch和Focus事件 * * 2)androidManifest里申明权限 * <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> * * * @author jiese1990 * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = new TextView(this); tv.setText("this is a desktop text"); tv.setTextColor(0xFFFF0000); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show(); } }); tv.setBackgroundColor(0xFF00000); initDestopText(tv); } private void initDestopText(View childView){ //直接通过Activity获取的 WindowManager,在act退出时,桌面组件也将退出。 // WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); //一定要通过getApplicationContext()获取WindowManager,这种情况下,当Application终止后,悬浮控件才会被退出 WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams(); // params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; //如果设置为 params.type = WindowManager.LayoutParams.TYPE_PHONE; // params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; wm.addView(childView, params); } }
标签:des android style color io os ar java for
原文地址:http://blog.csdn.net/jiese1990/article/details/40051649