【问题标题】:Android AppCompatActivity not showing toolbarAndroid AppCompatActivity 不显示工具栏
【发布时间】:2017-01-17 03:07:26
【问题描述】:

在我的应用程序中,我有一个带有抽屉布局的主 Activity,用于在两个片段之间导航。其中一个片段有一个 FAB,它打开另一个从 AppCompatActivity 扩展的活动。此活动没有显示下部工具栏,而上部工具栏显示为灰色。我设法在 xml 代码中添加了一个下部工具栏:

<android.support.v7.widget.Toolbar
    android:id="@+id/editor_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:title="Add Manual Entry"/>

但无论如何,上方的工具栏始终显示为灰色。尝试将上部工具栏设置为我的 colorPrimaryDark 作为一种解决方法,但没有办法做到这一点。我查看了我的 AppTheme 并发现了这个:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

看到父母说没有操作栏,我尝试将其更改为 Theme.AppCompat.Light 但我在主要活动中收到此错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tobias.run/com.example.tobias.run.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

看起来我的 MainActivity 及其抽屉布局需要 NoActionBar 主题,尽管我不确定。为什么我的次要活动没有工具栏?如何添加?

PS:辅助活动代码和 xml 都与 Android Studio 使用空活动模板创建它们的方式一样。 xml 刚刚添加了工具栏。

【问题讨论】:

    标签: android android-toolbar appcompatactivity


    【解决方案1】:

    试试这个

    <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="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
    </style>
    

    和你的清单

    <activity android:name=".activity.MyActivity"
              android:theme="@style/AppTheme.NoActionBar">
    

    【讨论】:

    • 这是他得到的错误中的最后一行 - Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 我认为 google 建议为了灵活性而继续使用工具栏方式。
    【解决方案2】:

    这意味着您已经在视图层次结构中的某个位置包含了一个工具栏(您还没有提供完整的布局,所以很难说)。 在任何情况下,只需将这两行粘贴到您的代码中(OnCreate 您的 Actvity),大多数情况下这应该可以解决您的问题。

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    哦,是的,恢复您为提供操作栏所做的其他更改。

    如果您在布局中专门更改了工具栏引用,它可能会有所不同。默认总是给定一个 id toolbar Android 有时会改进整个操作栏,使其更通用。因此,您在布局中包含的是一个通用工具栏,除非您在代码中设置它,否则它不会显示为操作栏。

    【讨论】:

      【解决方案3】:

      最终找到了解决方法。问题是 &lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt; 在应用程序主题中。将其设置为透明时,在激活抽屉布局时状态栏会淡出为透明,这是预期的结果。但是当您创建一个新活动时,状态栏看起来与您的背景完全一样,看起来好像没有状态栏。为了让它工作,我在应用程序主题中为抽屉布局保留了透明的 attr,但在运行时改变了,在我的其他活动中,我的状态栏的颜色在这个 post 之后。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-02
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多