开发者

android: set layout dimension, center it and set to fullscreen in combination does not work?

I want to set width and height of my window, center it and set it to fullscreen to hide the top bar.

With these lines I can set the dimension and set it to fullscreen but centering takes no effect.

Window window = getWindow(); 
window.setGravity(Gravity.CENTER | Gravity.FILL_VERTICAL | Gravity.FILL_HORIZONTAL); 
window.setLayout(480, 320); 
window.requestFeature(Window.FEATURE_NO_TITLE); 
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Following sets the dimension and centers it but the top bar is开发者_开发问答 visible so fullscreen takes no effect.

Window window = getWindow(); 
window.setGravity(Gravity.CENTER); 
window.setLayout(480, 320); 
window.requestFeature(Window.FEATURE_NO_TITLE); 
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

And fanally if I add

<application android:icon="@drawable/icon" android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

The window is centered, dimensions are set and it is fullscreen… but only the first time I launch the app. After the app is closed and started again, it isn't fullscreen anymore.

How can I achieve to get a sized, centered window in fullscreen?

@edit: I figured out that if I exit the app with the device back button it works always. The top bar gets visible only if I quit the app with the home button.


So I don't have a solution but a small hack. The following destroys the app when the home button was pressed. That forces the app to restart and hide the top bar again. I'm not happy with this hack but it works for now.

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onStop()
 */
@Override
protected void onStop()
{
    super.onStop();


    //hack to prevent stopping fullscreen mode if the home button was pressed
    ActivityManager am = (ActivityManager) this
                    .getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;

    if(componentInfo.getShortClassName().equalsIgnoreCase(".Launcher"))
    {
        onDestroy();
    }
}


@Override
public void onDestroy()
{
    super.onDestroy();
    System.runFinalizersOnExit(true);
    System.exit(0);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜