2012-11-16 38 views
1

我創建並顯示了一個Alertdialog,在仿真器中看起來很好,但在我的設備中看起來很奇怪。Android Alertdialog在Emultator和Device上有不同的大小

這是怎麼看起來像在模擬器:

enter image description here

這是怎麼樣子的設備:

enter image description here

這是代碼我正在執行以顯示對話框:

AlertDialog.Builder builder; 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    View layout = inflater.inflate(R.layout.popup_contact, (ViewGroup) findViewById(R.id.ppc_llContact)); 
    tbMissatge = (EditText) layout.findViewById(R.id.ppc_tbMissatge); 
    builder = new AlertDialog.Builder(this); 


    builder.setView(layout); 
    ppEnviarMsg = builder.create(); 


    btEnviar = (Button) layout.findViewById(R.id.ppc_btEnviar); 
    btEnviar.setOnClickListener(this); 

    ppEnviarMsg.show(); 


    ppEnviarMsg.getWindow().getAttributes().gravity = Gravity.CENTER; 

這裏彈出窗口的佈局:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ppc_llContact" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#414145" 
     android:orientation="vertical" 
     android:gravity="center"> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#ffffff" 
      android:text="@string/ppc_lbContact" 
      android:layout_margin="4dp"/> 

     <EditText 
      android:id="@+id/ppc_tbMissatge" 
      android:hint="@string/ppc_tooltipContact" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dp" 
      android:inputType="textMultiLine" 
      android:gravity="top" 
      android:lines="5"> 
     </EditText> 

     <Button android:id="@+id/ppc_btEnviar" 
      android:text="@string/ppc_btEnviar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:paddingLeft="20dp" 
      android:paddingRight="20dp" 
      android:layout_marginTop="10dp"/> 

    </LinearLayout> 

難道可能與意識有關?我的設備是HTC Desrie S,運行ICS和Sense。 有沒有人有線索?

感謝您的幫助!

+2

你可能也想嘗試在ICS模擬器中運行它。屏幕截圖中的模擬器看起來像2.3之前的東西.. – Ivan

+0

我建議你嘗試一個對話框片段,看看它的行爲。 – Warpzit

+0

感謝您編輯圖像。好吧,我會在模擬器中嘗試ICS,並且還會查看對話框片段。 我會發布結果! – weilah

回答

1

我認爲這個問題可能是在這裏:

View layout = inflater.inflate(R.layout.popup_contact, (ViewGroup) findViewById(R.id.ppc_llContact)); 

您使用一些佈局ppc_llContact你的活動是一個「對象,提供一組的LayoutParams值的返回的層次結構的根」,這是相當毫無意義,因爲這種佈局不會參與對話的最終層次結構。

使用這個代替:

View layout = inflater.inflate(R.layout.popup_contact, null); 

另一種方法是強制設置對話框的大小是這樣的:

ppEnviarMsg.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
//FILL_PARENT is an example 
+0

嘿,第二個選項做到了!現在問題出現在屏幕的頂部,儘管我使用'ppEnviarMsg.getWindow()。getAttributes()。gravity = Gravity.CENTER;'。你知道爲什麼嗎? – weilah

+0

如果您在我的示例中使用了'LayoutParams.FILL_PARENT',那麼發生這種情況是因爲它。 您可以改爲設置與屏幕大小相關的大小(例如,三分之二): 'View decorView = getWindow()。getDecorView(); ppEnviarMsg.getWindow()。setLayout(decorView.getWidth()* 2/3,decorView.getHeight()* 2/3);' –

+0

謝謝,很好!對此,我真的非常感激。我做了類似的事情,但使用了'Display display = getWindowManager()。getDefaultDisplay();'你的剪輯可能更好。 – weilah

相關問題