onCreate方法中的弹出窗口调用

时间:2018-09-05 12:02:03

标签: android popupwindow layout-inflater

创建活动后,我想显示一个弹出窗口。 我遵循other posts的相关规定,建议将一些代码放入post方法中。目前我有

    final PopupWindow poppers = new PopupWindow(this);
    final View popLayout = getLayoutInflater().inflate(R.layout.content_popup, null);
    poppers.setContentView(popLayout);

    poppers.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    poppers.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    poppers.setBackgroundDrawable(new BitmapDrawable());

    poppers.setFocusable(true);
    poppers.setOutsideTouchable(true);
    poppers.setElevation(3);

    popLayout.post(new Runnable(){
        public void run() {

            poppers.showAtLocation(llMain, Gravity.CENTER, 0, 0);
        }

永远不会命中run()中的代码。任何想法为什么会这样?

2 个答案:

答案 0 :(得分:1)

按以下方式调用

 llMain.post(new Runnable(){
        public void run() {

            poppers.showAtLocation(llMain, Gravity.CENTER, 0, 0);
        }
  

使用rootView代替popLayout。

答案 1 :(得分:1)

    try this and declare this first      PopupWindow popupWindow;


     LayoutInflater layoutInflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View customView = layoutInflater.inflate(R.layout.activity_home,null);
            //instantiate popup window
             popupWindow = new PopupWindow(customView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

 //display the popup window
            popupWindow.showAtLocation(binding.llMain, Gravity.CENTER, 0, 0);



But I suggest use custom Dialog:

public class FilterDialog extends Dialog implements View.OnClickListener{
    DialogMatchBinding binding;
    Context context;
    private LayoutInflater inflater;

    public FilterDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_match, null, false);
        setContentView(binding.getRoot());
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        // set click listner

    }

}