Set custom layout in popup window in android

前端 未结 6 1854
别那么骄傲
别那么骄傲 2020-12-05 23:42

I have a problem with popup window. I want to create popup window with my own layout. This is code:

public class PopupWindowView extends PopupWindow{

    P         


        
6条回答
  •  时光说笑
    2020-12-06 00:44

    try this code:

    private void showSortPopup(final Activity context, Point p) 
    {
           // Inflate the popup_layout.xml
           LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llSortChangePopup);
           LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View layout = layoutInflater.inflate(R.layout.sort_popup_layout, viewGroup);
    
           // Creating the PopupWindow
           changeSortPopUp = new PopupWindow(context);
           changeSortPopUp.setContentView(layout);
           changeSortPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
           changeSortPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
           changeSortPopUp.setFocusable(true);
    
           // Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
           int OFFSET_X = -20;
           int OFFSET_Y = 95;
    
           // Clear the default translucent background
           changeSortPopUp.setBackgroundDrawable(new BitmapDrawable());
    
           // Displaying the popup at the specified location, + offsets.
           changeSortPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
    
    
           // Getting a reference to Close button, and close the popup when clicked.
           Button close = (Button) layout.findViewById(R.id.close);
           close.setOnClickListener(new OnClickListener() {
    
             @Override
             public void onClick(View v) {
               changeSortPopUp.dismiss();
             }
           });
    
    }
    

提交回复
热议问题