Android Implementation Hover control

Source: Internet
Author: User


Implementing Desktop Viewsuch as desktop lyrics

1) The view to be displayed on the desktop, through the Windowmanager.addview, hanging under the WindowManager;          Note that the source of the WindowManager object, the source code is explained. and configure the windowmanager.layoutparams of the view
1-1) WindowManager.LayoutParams.type is set to WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; The desktop UI cannot be directly setonclicklistener (), and the View's Ontouchevent () function is not set to WindowManager.LayoutParams.TYPE_SYSTEM_ALERT (2003). or WindowManager.LayoutParams.TYPE_PHONE (2002) will be effective.
1-2) WindowManager.LayoutParams.flag set to Layoutparams.flag_not_touch_modal | Layoutparams.flag_not_focusable
If WindowManager.LayoutParams.type is set to 2002,2003, then flag is not set to these two flags, then other components will not be able to get touch and focus events.
Once you have set the two properties, you can get touch and focus events

2) Declaration of authority in Androidmanifest
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

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;/** * Realize Desktop view * * 1) The view to be displayed on the desktop, through the Windowmanager.addview, hangs under the WindowManager; note that the source of the WindowManager object is explained in the source code. * and configured with view Windowmanager.layoutparams * 1-1) WindowManager.LayoutParams.type set to WindowManager.LayoutParams.TYPE_ System_overlay; * Desktop UI cannot be directly setonclicklistener (), the view's Ontouchevent () function is also invalid * set to WindowManager.LayoutParams.TYPE_ System_alert (2003) or WindowManager.LayoutParams.TYPE_PHONE2002 only works. * 1-2) WindowManager.LayoutParams.flag set to Layoutparams.flag_not_touch_modal | Layoutparams.flag_not_focusable * If WI is setNdowManager.LayoutParams.type is 2002,2003; If flag is not set to these two flags, then other components will not be able to get touch and focus events. * After setting the two attributes, you can get touch and focus events * 2) androidmanifest Declaration of Authority * <uses-permission android:name= " Android.permission.SYSTEM_ALERT_WINDOW "/> * * * @author jiese1990 * */public class Mainactivity extends Activity {@O verrideprotected 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 () {@ overridepublic void OnClick (View v) {toast.maketext (Mainactivity.this, "click", Toast.length_short). Show ();}); Tv.setbackgroundcolor (0xff00000); Initdestoptext (TV);} private void Initdestoptext (View childview) {//WindowManager is obtained directly through activity, and the desktop component exits when act exits. WindowManager wm = (WindowManager) getsystemservice (Window_service); Be sure to get the WindowManager through Getapplicationcontext (), in which case the hover control will be exited when the application terminates WindowmanaGer wm = (WindowManager) getapplicationcontext (). Getsystemservice (Window_service);  Windowmanager.layoutparams params = new Windowmanager.layoutparams ();  Params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; If set to 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); }}


Android Implementation Hover control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.