2012-07-09 113 views
7

我有一個從對話框擴展的自定義對話框。 爲了放置組件,我需要知道對話框的寬度。我怎麼能這樣做? 感謝如何在android中獲取自定義對話框的大小?

編輯:

public class AnswerTypeDialog extends Dialog{ 
    public AnswerTypeDialog(Context context) { 
     super(context); 
     mContext = context; 
    } 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     init(); 
    } 

    protected void init(){ 
     ll=(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.answertype_layout, null); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(ll); 
     mWindowWidth = this.getWindow().getAttributes().width; 
    } 
} 

mWindowWidth爲零。

+0

它取決於你給對話框的高度寬度。 – AkashG 2012-07-09 09:34:38

+0

@seaplain你知道嗎? – anuj 2014-04-11 17:34:55

+0

試圖改變你的想法。使用資源標識並根據屏幕大小創建多個佈局。 – vladaman 2014-12-14 19:31:47

回答

-2

您可以使用此代碼段

int height=mDialog.getWindow().getDecorView().getHeight(); 
int width=mDialog.getWindow().getDecorView().getWidth(); 
+1

但它返回0. – shohreh 2012-07-09 09:47:01

+0

@shohreh向我們展示一些代碼你在做什麼 – Akram 2012-07-09 09:47:57

+4

這隻返回與其屬性相關的常量,例如-2 = WRAP_CONTENT。這個答案是無用的。 – slezadav 2012-11-22 15:16:09

0

這將工作。

@Override 
    public void onWindowFocusChanged (boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     int height=mDialog.getWindow().getDecorView().getHeight(); 
    int width=mDialog.getWindow().getDecorView().getWidth(); 
} 
相關問題