0

採用沉浸式模式我嘗試讓應用程序,將進入身臨其境的模式景觀(不包含狀態欄和導航欄)和portrate(與返回狀態欄和導航欄)禁用身臨其境的模式。沒有重新啓動活動(onCreate只調用一次)。 我寫了一個小示例應用程序來顯示我的問題。只在橫向模式

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="test.com.immersivetest"> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:configChanges="orientation|screenSize"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:padding="0dp" 
    tools:context="test.com.immersivetest.MainActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffff0000" 
     /> 
</RelativeLayout> 

MainActivity.java

package test.com.immersivetest; 

import android.content.res.Configuration; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 

    private View activity; 
    private View decorView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     activity = findViewById(R.id.activity_main); 
     decorView = this.getWindow().getDecorView(); 
     System.out.println("On Crate"); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     updateForOrientation(newConfig.orientation); 
    } 

    public void updateForOrientation(int orientation) { 
     if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      hideSystemUI(); 
     } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { 
      showSystemUI(); 
     } 
    } 



    private void hideSystemUI() { 
     System.out.println("Hide UI"); 
     activity.setFitsSystemWindows(false); 
     decorView.setSystemUiVisibility(
       View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
         | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
         | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 
         | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar 
         | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 

    } 

    private void showSystemUI() { 
     activity.setFitsSystemWindows(true); 
     decorView.setSystemUiVisibility(
       View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
         | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
    } 


} 

個步驟,看看我的問題:

1)啓動應用程序(在縱向模式下),並在結果我看到孩子的RelativeLayout上全屏紅色背景的標題欄,狀態欄,導航欄。這是正確的。屏幕截圖

2)旋轉到橫向。再次,我看到正確的結果:Child RelativeLayout紅色背景沉浸模式沒有標題欄,狀態欄,導航欄。

3)旋轉到肖像。並看到正確的結果:像第一步截圖一樣。

4)旋轉爲橫向一次。我看到不正確的結果。 Child RelativeLayout未拉伸到父級大小。我們在屏幕的頂部和底部看到空的字段。截圖與問題

如何解決呢?爲什麼所有的旋轉將第一個不正確調整大小的孩子RelativeLayout調整爲橫向?

P.s.閔SDK版本19.

回答

0

,我發現自己的解決方案;)我從https://developer.android.com/training/system-ui/immersive.html#nonsticky了片段。它不正確。我在showSystemUI()中更改了標誌,並且所有的作品都完美無缺!

private void showSystemUI() { 
    activity.setFitsSystemWindows(true); 
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); 
} 

但它在我的主要應用程序的工作,因爲我不使用操作欄。如果啓用了操作欄,則操作欄顯示不正確。我不知道爲什麼。任何方式我檢查啓動應用程序的標誌,它只有SYSTEM_UI_FLAG_VISIBLE。

如果任何人有與動作條的應用解決方案,我會很高興地知道解決方案。