2017-06-18 7 views
0

Ich begann neu Android zu lernen. Um eine kompatible Version mit Android Lollipop (API 21) und auch Android Pre-Lollipop-Geräten (API15) zu entwickeln, definiere ich zwei verschiedene Stile. Der erste Stil unterstützt ActionBar und der andere erweitert die Eigenschaft .NoActionBar, die für API21 und höher benötigt wird. Ich denke, dass ich die Appbar mit Toolbar und Actionbar missverstehen kann.Hide Symbolleiste (in einem AppBarLayout) in einem bestimmten Fragment

Zusätzlich verwende ich zwei Fragmente in meiner Haupttätigkeit. Ich frage mich, ob es eine Möglichkeit gibt, die Symbolleiste (die sich in der AppBarLayout in der XML-Datei befindet) in einem bestimmten Fragment zu verstecken und im anderen Fragment anzuzeigen.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example"> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     ... 
     ... 
    </application> 
</manifest> 

app_bar_main.xml

<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 
      <com.example.MyTextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="22sp" 
       android:layout_gravity="center" 
       android:text="@string/app_name"/> 
     </android.support.v7.widget.Toolbar> 

</android.support.design.widget.AppBarLayout> 

styles.xml

<resources> 
    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="windowNoTitle">true</item> 
    </style> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

</resources> 

styles.xml (v21)

<resources> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
    </style> 
</resources> 

Antwort

0

Die erste Art ActionBar und die andere unterstützt die Eigenschaft erstreckt, die für .NoActionBar API21 und darüber benötigt wird.

ActionBar wird nicht mehr empfohlen, so dass Sie Toolbar überall verwenden können. Erweitere deine Aktivität um AppCompatActivity und setze deine Toolbar als Actionbar mit setSupportActionBar (Toolbar toolbar)

+0

Aber für die Pre-Lollipop-Geräte, wird die Symbolleiste unterstützt? –

+0

@invertierte_Index machen Ihre Aktivität erweitern AppCompatActivity, die in der Support-Bibliothek enthalten ist, dann können Sie Toolbar überall verwenden – Darish

+0

Meinst du, dass ich die 'ActionBar' aus der styles.xml überhaupt entfernen kann? –