Android popupwindow simulates Sina and Tencent title tabs

Source: Internet
Author: User

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

Related Article

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.