2017-06-18 110 views
0

我刚开始学习android。为了开发与Android棒棒糖(API 21)兼容的版本以及android棒棒糖设备(API15),我定义了两种不同的样式。第一种风格支持ActionBar,另一种风格扩展了API21及更高版本所需的属性.NoActionBar。我认为我可能会误解与工具栏和操作栏的appbar。隐藏工具栏(在AppBarLayout里面)中的一个特定片段

另外,我在我的主要活动中使用了两个片段。我想知道是否有工具栏(这是在XML文件中的AppBarLayout内)被隐藏在特定片段中并显示在其他片段中的方式。

的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> 

回答

0

第一样式支持动作条,而另一个延伸这是需要API21和上述属性.NoActionBar。

不再推荐使用ActionBar,因此您可以随处使用Toolbar。使您的活动延伸AppCompatActivity并将您的工具栏设置为操作栏使用setSupportActionBar (Toolbar toolbar)

+0

但是对于预棒棒糖设备,是否支持工具栏? –

+0

@inverted_index让你的活动延伸到支持库中包含的AppCompatActivity,然后你可以在任何地方使用工具栏 – Darish

+0

你的意思是我可以从styles.xml中移除'ActionBar'吗? –