Custom Popwindow: Custom popwindow
Popwindow is a commonly used control. For flexible use, popwidow is customized to inherit from popwindow.
This article mainly introduces how to customize popwindow and respond to view events in popwindow.
1. Define MenuPop to inherit from popwindow
The Code is as follows:
Public class MenuPopwindow extends PopupWindow implements OnClickListener {private Context context; private View mMenu; private TextView tv1, tv2, tv3, tv4; public MenuPopwindow (Context context) {// this line of code must not be forgotten // otherwise, NullPointExceptionsuper (context); this will occur during initialization. context = context; // view control initView (); initData ();} private void initView () {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); mMenu = inflater. inflate (R. layout. menu_main, null); tv1 = (TextView) mMenu. findViewById (R. id. menu_1); tv2 = (TextView) mMenu. findViewById (R. id. menu_2); tv3 = (TextView) mMenu. findViewById (R. id. menu_3); tv4 = (TextView) mMenu. findViewById (R. id. menu_4); tv1.setOnClickListener (this); tv2.setOnClickListener (this); tv3.setOnClickListener (this); tv4.setOnClickListener (this);} private void initData () {// set the background of popwindow this. setContentView (mMenu); // Set width, height this. setWidth (LayoutParams. MATCH_PARENT); this. setHeight (LayoutParams. WRAP_CONTENT); // this sentence is critical. The focus will be obtained when popwindow is displayed. Clicking other regions will disappear. // if it is set to false, clicking other regions will not disappear this. setFocusable (true); // set the background ColorDrawable colorDrawable = new ColorDrawable (0Xb0000000) of popup; this. setBackgroundDrawable (colorDrawable);} @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. menu_1: SetToast. setToastShort (context, "my eldest brother"); break; case R. id. menu_2: SetToast. setToastShort (context, ""); break; case R. id. menu_3: SetToast. setToastShort (context, "My brother three"); break; case R. id. menu_4: SetToast. setToastShort (context, "my younger brother"); break; default: break ;}}}
The usage of popwindow is as follows:
Popwindow is displayed in the Activity click event.
The Code is as follows:
<Pre name = "code" class = "html"> // initialize MenuPopwindow popwindow = new MenuPopwindow (this); // call the first parameter where popwindow needs to be popped up, the pop-up relative parent view, btnView // second, position // The third and fourth offset to the parent popwindow. showAtLocation (btnView, Gravity. BOTTOM, 0, 0 );
Such a simple popwindow is defined. The display effect is as follows:
Is there any control in the layout not directly obtained through findviewbyid?
Different layout. the spatial id in xml can be the same. You can use layout1.findViewById (100) and layout2.findViewById (100) to obtain the corresponding layout control. This will be automatically distinguished and you don't need to worry about it.
Android 23 + how to implement the menu of the custom background,
Some mobile phones can be set. If you cannot set a 360 mobile phone desktop or other beautifying desktops, you can customize the menu background.