【问题标题】:Hide toolbar (inside a AppBarLayout) in a specific fragment在特定片段中隐藏工具栏(在 AppBarLayout 内)
【发布时间】:2017-11-20 14:40:31
【问题描述】:

我刚开始学习android。为了开发与 Android Lollipop (API 21) 和 android pre-lollipop 设备 (API15) 兼容的版本,我定义了两种不同的样式。第一种样式支持 ActionBar,另一种扩展了 API21 及更高版本所需的属性.NoActionBar。我想我可能误解了带有工具栏和操作栏的 appbar。

此外,我在主要活动中使用了两个片段。我想知道是否有一种方法可以将工具栏(位于 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>

【问题讨论】:

    标签: android xml android-fragments toolbar appbar


    【解决方案1】:

    第一种样式支持ActionBar,另一种扩展了API21及以上需要的属性.NoActionBar。

    不再推荐使用ActionBar,因此您可以在任何地方使用Toolbar。使您的活动扩展 AppCompatActivity 并使用 setSupportActionBar (Toolbar toolbar) 将您的工具栏设置为操作栏

    【讨论】:

    • @inverted_index 使您的活动扩展包含在支持库中的 AppCompatActivity,然后您可以在任何地方使用工具栏
    • 你的意思是我可以从styles.xml中删除ActionBar吗?
    • 你不需要编辑当前的样式,你可以在整个应用程序中使用AppTheme.NoActionBar主题
    • 你贴的代码是android studio生成的吧?你不必做任何事情。 Android Studio 会为你做这件事。只需在 Manifest 文件中将 AppTheme.NoActionBar 设置为 Activity 主题。干杯:)
    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多