First, the dialog is implemented in the previous section (click to connect). Now I will introduce the implementation of popupwindow, which is relatively simple to dialog because custom dialog is not required.
The implementation code is as follows:
Code snippet:
[Java]
Public void showPopupWindow (int x, int y ){
Layout = (LinearLayout) LayoutInflater. from (MainActivity. this). inflate (
R. layout. dialog, null );
ListView = (ListView) layout. findViewById (R. id. lv_dialog );
ListView. setAdapter (new ArrayAdapter <String> (MainActivity. this,
R. layout. text, R. id. TV _text, title ));
PopupWindow = new PopupWindow (MainActivity. this );
PopupWindow. setBackgroundDrawable (new BitmapDrawable ());
PopupWindow
. SetWidth (getWindowManager (). getdefadisplay display (). getWidth ()/2 );
PopupWindow. setHeight (300 );
PopupWindow. setOutsideTouchable (true );
PopupWindow. setFocusable (true );
PopupWindow. setContentView (layout );
// ShowAsDropDown uses the view in it as a reference object, so you need to fill the screen with parent
// PopupWindow. showAsDropDown (findViewById (R. id. TV _title), x, 10 );
PopupWindow. showAtLocation (findViewById (R. id. main), Gravity. LEFT
| Gravity. TOP, x, y); // you must specify Gravity. The default value is center.
ListView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
Button. setText (title [arg2]);
PopupWindow. dismiss ();
PopupWindow = null;
}
});
}
[Java]
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Button. getTop ();
Int y = button. getBottom () * 3/2;
Int x = getWindowManager (). getdefadisplay display (). getWidth ()/4;
ShowPopupWindow (x, y );
} Www.2cto.com
});
As shown in the previous chapter dialog.
There is a small problem: int y = button. getBottom () * 3/2; The y coordinate obtained here should be a button. getBottom (); but in this case, the position of the popupwindow is incorrect. In the middle of the button, I don't know why.
If you know why, let me know and learn together. Thank you first.
Author: jj120522