【问题标题】:AppCompactv21 Toolbar as ActionBar theming has no affectAppCompactv21 工具栏作为 ActionBar 主题没有影响
【发布时间】:2014-11-21 12:54:34
【问题描述】:

我已遵循指南http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html 并将工具栏实现为 ActionBar,但我似乎无法更改工具栏的主题。我使用 Theme.AppCompat.Light 作为我应用的基本主题,并希望单独设置样式/主题工具栏/操作栏。

向我的 app_coomon_toolbar_xml 添加样式完全没有影响 例如当我在下面添加时

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"  

它的 app_common_toolbar.xml 主题没有改变,它仍然是从主应用程序主题中挑选出来的,即 Theme.AppCompat.Light。

仅当我将 App 基本主题更改为 Theme.AppCompat.Light.DarkActionBar 时,工具栏主题才会更改。我不想那样做。 我做错了什么?


这里是我的工具栏的详细信息以及我如何加载它

app_common_toolbar.xml

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/    android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_common_toolbar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"    
/>

MainActivity.java super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_common_toolbar);

    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }

【问题讨论】:

  • 如果您要调用setSupportActionBar,那么您的主题不应该有操作栏。
  • 你的意思是我不应该将主题与像 ThemeOverlay.AppCompat.Dark.ActionBar 这样的操作栏一起使用吗?
  • 按您的方式尝试。您将在setSupportActionBar 上收到一条错误消息,告诉您您已经有一个操作栏。
  • 不,它没有,它工作正常
  • 不确定这怎么可能。在这里查看我的模板https://github.com/kanytu/android-material-drawer-template。它使用没有操作栏的主题。

标签: android android-appcompat


【解决方案1】:

我是这样解决的:

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/my_toolbar" android:layout_height="128dp" app:popupTheme="@style/ActionBarPopupThemeOverlay" android:layout_width="match_parent" android:background="?attr/colorPrimary" android:paddingLeft="72dp" android:paddingBottom="16dp" android:gravity="bottom" app:titleTextAppearance="@style/Toolbartitle" app:subtitleTextAppearance="@style/ToolbarSubtitle" app:theme="@style/ThemeOverlay.AppCompat.Light" android:title="@string/location_placeholder" />

并像定义colors.xml

    <resources>
      <color name="primaryColor_500">#03a9f4</color>
      <color name="primaryDarkColor_700">#0288d1</color>    
    </resources>

最后是 style.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primaryColor_500</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor_700</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

希望对你有帮助!

【讨论】:

  • Theme.AppCompat.Light.NoActionBar 在我尝试设置 actionBar.setHomeButtonEnabled(true) 时导致崩溃
【解决方案2】:

根据 Pedro Oliveira 的 cmets,我正在对 actionbar 进行 null 检查,因此甚至没有调用 setSupportactionBar。我现在可以按照他在 Github 上的示例进行必要的更改

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多