2017-07-27 40 views
0

我的應用程序中有一個PopupWindow。 這就是我初始化:PopupWindow不顯示

final FrameLayout frameLayout = new FrameLayout(context); 
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(200, 200)); 
hashTagsWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
hashTagsWindow.setBackgroundDrawable(new ColorDrawable(Color.BLACK)); 

這就是我如何努力表現出來:

hashTagsWindow.showAsDropDown(binding.hash, 20, 20); 

但我PopupWindow不顯示自身。

我已經嘗試解決一個問題?:

  • .showAsDropDown(...)到PopupWindow的應該表現出來的.post(Runnable).runOnUiThread(Runnable)

  • 所有方法。

  • PopupWindowCompat.showAsDropdown(...)方法。

所有這些事情都沒有幫助我。

同時一個PopupMenu的類正確地顯示在相同的上下文。但我需要PopupWindow。

我應該怎麼做才能顯示PopupWindow?

+0

你叫.show()因爲我看不出它有 – ADM

+0

PopupWindow沒有.show()方法 –

+0

對不起我得到ListPopupWindow混淆。 – ADM

回答

0

按照這種方法,可以幫助你。

private void showPopupWindow(View view){ 

    LayoutInflater mLayoutInflater=LayoutInflater.from(this); 
    View mView=mLayoutInflater.inflate(R.layout.pop_up_layout, null); 

    mPopupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); 
    // mPopupWindow=new PopupWindow(mView,,LayoutParams.WRAP_CONTENT); 
    mPopupWindow.setContentView(mView); 

    Button mBtnCancel=(Button) mView.findViewById(R.id.btn_cancel); 
    mBtnCancel.setOnClickListener(this); 

    mPopupWindow.showAsDropDown(view, 0, 0, Gravity.LEFT); 

    mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 
     @Override 
     public void onDismiss() { 
      Drawable d = new ColorDrawable(Color.WHITE); 
      getWindow().setBackgroundDrawable(d); 

     } 

    }); 

} 
+1

如果您對代碼的作用做了一些解釋,那會更好嗎? – AguThadeus

相關問題