0

Ich versuche App zu erstellen, die in den immersiven Modus im Querformat (ohne Statusleiste und Navigationsleiste) wechselt und den Immersive Modus auf Portrate deaktiviert (mit Statusleiste und Navigationsleiste). Ohne Neustart-Aktivität (OnCreate nur einmal aufgerufen). Ich schrieb kleine Beispiel-App, um mein Problem zu zeigen.Verwenden des Immersiven Modus nur im Querformat

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


} 

Schritte, um mein Problem zu sehen:

1) Starten Sie App (im Hochformat) und im Ergebnis sehe ich Kind RelativeLayout mit rotem Hintergrund auf Vollbild mit Titelleiste, Statusleiste, Navigationsleiste. Es ist richtig. Screenshot

2) In Querformat drehen. Und wieder sehe ich das korrekte Ergebnis: Child RelativeLayout mit rotem Hintergrund im immersiven Modus ohne Titelleiste, Statusleiste, Navigationsleiste.

3) Zum Hochformat drehen. Und sehen Sie korrekte Ergebnisse: wie auf Screenshot für den ersten Schritt.

4) Zum Querformat drehen. Und ich sehe nicht korrektes Ergebnis. Child RelativeLayout wurde nicht auf die übergeordnete Größe erweitert. Wir sehen leere Felder am oberen und unteren Bildschirmrand. Screenshot mit Problem

Wie es zu beheben? Warum alle Rotationen in Querformat des ersten nicht korrekt skalierten untergeordneten Elements RelativeLayout?

Ps.s. Min sdk Version 19.

Antwort

0

Ich fand Lösung selbst;) Ich habe Schnipsel von https://developer.android.com/training/system-ui/immersive.html#nonsticky. Und es funktioniert nicht richtig. Ich habe Flags in showSystemUI() geändert und alles funktioniert perfekt!

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

Aber es funktioniert in meiner Haupt-App, weil ich keine Aktionsleiste verwenden. Wenn Aktionsleiste aktiviert ist, wird die Aktionsleiste nicht korrekt angezeigt. Ich weiß nicht warum. Irgendwie habe ich Flags in der Start-App überprüft und es hat nur SYSTEM_UI_FLAG_VISIBLE.

Wenn jemand Lösung für App mit Aktionsleiste habe, werde ich glücklich sein, Lösung zu wissen.

Verwandte Themen