Custom Popwindow: Custom popwindow

Source: Internet
Author: User

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.

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.