onCreate方法中的弹出窗口调用

问题描述 投票:0回答:2

我想在创建活动时显示一个弹出窗口。我跟着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()中的代码永远不会被命中。任何想法为什么会这样?

android popupwindow layout-inflater
2个回答
1
投票

请将其命名如下

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

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

而不是popLayout使用您的rootView。


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

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.