The pop-up box in the upper-right corner of the android popwindow is displayed at the bottom of the dialog
Like the pop-up box in the upper-right corner 1. Use popwindow to implement 2. Create popwindow in the upper-right corner and pop-up popwindow:
/** Play popwindow **/TV = (TextView) findViewById (R. id. textView1); view_pop = LayoutInflater. from (MainActivity. this ). inflate (R. layout. pop_menu, null); TV. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {if (mPopupwinow = null) {// create a new listener = new PopupWindow (view_pop, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, true); // set the background color of the popwindow to mPopupwinow. setBackgroundDrawable (new ColorDrawable (0x00000000);} // set the position of popwindow to TV: + view in the upper right corner and mPopupwinow below +. showAsDropDown (TV, 0, 0 );}});
Layout pop_menu.xml:
Effect:
1. Create a dialog at the bottom of the screen and set the location of the dialog.
/** Play dialog **/private void showDialog () {dialog = new Dialog (this, R. style. dialog); view_dialog = LayoutInflater. from (MainActivity. this ). inflate (R. layout. dialog_layout, null); dialog. setContentView (view_dialog); TextView TV _cancle = (TextView) view_dialog.findViewById (R. id. TV _cancle); TV _cancle.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {dialog. dismiss () ;}});/** get the window object and parameter object of the Christmas box to modify the layout settings of the dialog box. You can directly call getWindow (), the Window * object of the Activity, which can be changed in the same way. */Window dialogWindow = dialog. getWindow (); // set the location dialogWindow. setGravity (Gravity. BOTTOM); // set the dialogWindow attribute of the width and height of the dialog. setLayout (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); dialog. show ();}
Xml:
R. style. dialog:
:
MainActivity. java:
Package com. yqy. yqy_popwindow; import android. app. activity; import android. app. dialog; import android. graphics. drawable. colorDrawable; import android. OS. bundle; import android. view. gravity; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup. layoutParams; import android. view. window; import android. view. windowManager; import android. widget. button; import android. widget. popupWindow; import android. widget. textView; public class MainActivity extends Activity {private TextView TV; private View view_pop, view_dialog; private PopupWindow layout = null; private Button btn_dialog; private Dialog dialog; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/** popwindow **/TV = (TextView) findViewById (R. id. textView1); view_pop = LayoutInflater. from (MainActivity. this ). inflate (R. layout. pop_menu, null); TV. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {if (mPopupwinow = null) {// create a new listener = new PopupWindow (view_pop, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, true); // set the background color of the popwindow to mPopupwinow. setBackgroundDrawable (new ColorDrawable (0x00000000);} // set the position of popwindow to TV: + view in the upper right corner and mPopupwinow below +. showAsDropDown (TV, 0, 0) ;}}); btn_dialog = (Button) findViewById (R. id. button1); btn_dialog.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {showDialog ();}});} /** play dialog **/private void showDialog () {dialog = new Dialog (this, R. style. dialog); view_dialog = LayoutInflater. from (MainActivity. this ). inflate (R. layout. dialog_layout, null); dialog. setContentView (view_dialog); TextView TV _cancle = (TextView) view_dialog.findViewById (R. id. TV _cancle); TV _cancle.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {dialog. dismiss () ;}});/** get the window object and parameter object of the Christmas box to modify the layout settings of the dialog box. You can directly call getWindow (), the Window * object of the Activity, which can be changed in the same way. */Window dialogWindow = dialog. getWindow (); // set the location dialogWindow. setGravity (Gravity. BOTTOM); // set the dialogWindow attribute of the width and height of the dialog. setLayout (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); dialog. show () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}