Theme.Dialog creates too small screen

大憨熊 提交于 2019-12-10 15:47:06

问题


I have an activity with ListView that has:

android:theme="@android:style/Theme.Dialog"

in Manifest. When I open it and when it has only one line in ListView, the window that opens is very small. How do I make the window take the whole screen?


回答1:


Use this in your onCreate method of the Activity to make it full screen.

   @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);
   } 



回答2:


I have found that setting the window size does work, but you have to do it a bit later. In this example the window width is set to 90% of the display width, and it is done in onStart() rather than 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);
}



回答3:


Similar to the answer from PravinCG but it can be done with one line in onCreate()...

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



回答4:


Use the suggested code before setcontentview() call. It will work.




回答5:


Just a small update. Used MATCH_PARENT instead of the deprecated FILL_PARENT. PravinCG's answer worked great for me.




回答6:


Yeezz ! I figured it out ! The problem is that the margin sizes are not calculated in the window widht. So If you set the layout margin to 0 and move that part to the padding of the layout the problem will be solved.



来源:https://stackoverflow.com/questions/5969184/theme-dialog-creates-too-small-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!