2011-05-11 107 views
4

我有ListView中有一個活動:Theme.Dialog創建太小屏幕

android:theme="@android:style/Theme.Dialog" 
在清單

。 當我打開它並且它在ListView中只有一行時,打開的窗口非常小。 如何讓窗戶佔據整個屏幕?

回答

7

在Activity的onCreate方法中使用它來使其全屏。

@Override 
protected void onCreate(Bundle arg0) { 
    super.onCreate(arg0); 
    setContentView(R.layout.myxml); 

    LayoutParams params = getWindow().getAttributes(); 
      params.height = LayoutParams.MATCH_PARENT; 
      params.width = LayoutParams.MATCH_PARENT; 
      getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
    } 
+0

解決方案不起作用。也許這是由於ListView的對話框主題?有任何想法嗎? – OkyDokyman 2011-05-11 20:03:34

+0

謝謝!我的活動(通過Manifest看起來像是一個對話框)有一個相當複雜的LinearLayout,只顯示幾個字母的寬度。但哇,它爲我工作!但爲什麼? – 2012-10-19 05:48:21

+0

這不行! – JaydeepW 2014-04-18 05:42:00

1

從PravinCG答案相似,但它可以用一個線的onCreate()來完成...

getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
+0

解決方案無法正常工作。也許這是由於ListView的對話框主題?有任何想法嗎? – OkyDokyman 2011-05-11 20:03:44

0

使用的setContentView()調用之前建議的代碼。它會工作。

3

我發現設置窗口大小確實有效,但您必須稍後再做。在此實例中的窗口寬度被設置爲顯示寬度的90%,並且在onStart()而非onCreate()完成:

@Override 
protected void onStart() { 
    super.onStart(); 
    // In order to not be too narrow, set the window size based on the screen resolution: 
    final int screen_width = getResources().getDisplayMetrics().widthPixels; 
    final int new_window_width = screen_width * 90/100; 
    LayoutParams layout = getWindow().getAttributes(); 
    layout.width = Math.max(layout.width, new_window_width); 
    getWindow().setAttributes(layout); 
} 
0

只是一個小的更新。使用MATCH_PARENT而不是已棄用的FILL_PARENT。 PravinCG的答案對我很好。

0

Yeezz!我想到了 !問題是邊距大小不在窗口widht中計算。所以,如果您將佈局邊距設置爲0並將該部分移至佈局的填充處,則問題將得到解決。