Theme.Dialog creates too small screen
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 th开发者_如何学JAVAat opens is very small. How do I make the window take the whole screen?
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);
}
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);
}
Similar to the answer from PravinCG but it can be done with one line in onCreate()...
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Use the suggested code before setcontentview() call. It will work.
Just a small update. Used MATCH_PARENT instead of the deprecated FILL_PARENT. PravinCG's answer worked great for me.
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.
精彩评论