2013-03-26 44 views
1

我想在我的應用程序來改變屏幕布局,全屏模式下的按鈕,用戶點擊,但它沒有工作設置爲全屏,我的代碼是:如何屏幕大小在運行時的Android

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnFullScreen = (Button) findViewById(R.id.btnFullScreen); 
    btnNormalScreen = (Button) findViewById(R.id.btnNormalScreen); 

    btnFullScreen.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      setTheme(R.style.AppBaseThemeFullScreen); 

     } 
    }); 

    btnNormalScreen.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      setTheme(R.style.AppBaseTheme); 

     } 
    }); 



} 

而且我全屏主題是:

<style name="AppBaseThemeFullScreen" parent="android:Theme.Light"> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowNoTitle">true</item> 
</style> 

而且我一般主題

<style name="AppBaseTheme" parent="android:Theme.Light"> 
    <item name="android:windowNoTitle">true</item> 
</style> 

所以,如果有什麼辦法可以做到這一點,請幫助我。

+0

有沒有在logcat的東西嗎? – Cornholio 2013-03-26 09:21:58

+3

我想你會需要使用'parent =「android:Theme.Black.NoTitleBar.Fullscreen」'而不是'parent =「android:Theme.Light」'全屏? – 2013-03-26 09:25:49

+0

檢查這一個:http://stackoverflow.com/questions/7843825/how-to-set-xml-fullscreen-in-android – ophintor 2013-03-26 09:26:32

回答

10
void toggleFullScreen(boolean goFullScreen){ 
    if(goFullScreen){ 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
    }else{ 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 

    yourView.requestLayout(); 
} 
+0

非常感謝它的工作,非常感謝 – 2013-03-26 11:37:17

+0

你的歡迎兄弟 – Triode 2013-03-26 11:37:58

0

如果您想從代碼更改主題,看起來您必須先撥打電話setContentView()

你還可以嘗試:

requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
          WindowManager.LayoutParams.FLAG_FULLSCREEN);