在 onCreate 方法中弹出 Window 调用
Popup Window call within onCreate method
我想在创建 activity 时显示弹出窗口 Window。
我已经关注 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);
}
运行() 中的代码永远不会被命中。任何想法为什么会这样?
如下称呼
llMain.post(new Runnable(){
public void run() {
poppers.showAtLocation(llMain, Gravity.CENTER, 0, 0);
}
instead of popLayout use your rootView.
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
}
}
我想在创建 activity 时显示弹出窗口 Window。 我已经关注 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);
}
运行() 中的代码永远不会被命中。任何想法为什么会这样?
如下称呼
llMain.post(new Runnable(){
public void run() {
poppers.showAtLocation(llMain, Gravity.CENTER, 0, 0);
}
instead of popLayout use your rootView.
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
}
}