【问题标题】:Problems styling ActionBar background with Drawable使用 Drawable 设置 ActionBar 背景的问题
【发布时间】:2018-09-23 22:15:41
【问题描述】:

我尝试使用此主题/样式将图像添加到 ActionBar 背景

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@drawable/actionbar_background</item>
</style>

虽然背景正在改变,但它在标题和菜单按钮上产生了奇怪的效果。就像认为它们是 ActionBar 中的单独子视图(也许它们是,我不确定)并且 Drawable 被单独应用于它们。

我希望在 ActionBar 上应用无缝的 Drawable。这是可能的还是我应该放弃,而只是用纯色改变背景?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout_activity_main"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/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"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

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

styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="coordinatorLayoutStyle">@style/Widget.Support.CoordinatorLayout</item>
        <item name="colorAccent">@android:color/black</item>
        <item name="colorPrimary">@android:color/holo_blue_dark</item>
        <item name="colorPrimaryDark">@android:color/holo_blue_dark</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="android:textColorHint">@android:color/white</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="android:windowBackground">@drawable/no_actionbar_background</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:background">@android:color/black</item>
    </style>
</resources>

【问题讨论】:

  • 你能提供一些代码吗? style.xml,您的活动的 xml 等。我能够复制您的问题,但我无法猜测您的实现。
  • 没有必要显示任何 java 代码,因为我目前没有在其中做任何事情。在开始主要编码之前,我正在尝试整理主题。这一切都在styles.xml 中。我只需要将平滑的图像应用到标准操作栏。

标签: android android-actionbar android-theme


【解决方案1】:

当您设置视图的android:theme 属性时,您传递的样式将应用于该视图及其所有子视图。在您的情况下,AppBarLayout 确实(在内部)为标题和溢出菜单按钮使用单独的视图。

但是,由于您的样式似乎唯一要做的就是设置 android:background 属性,您可以直接在 AppBarLayout 上设置它。所以,替换这个:

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

用这个:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/actionbar_background">

【讨论】:

  • 有关主题与样式的更多信息,请参阅stackoverflow.com/a/48844052/8298909
  • 是的,我知道我可以将drawable直接添加到activity_main.xml中的appBarLayout,但我希望styles.xml中有一种方法可以将背景直接应用到应用栏背景而不影响它children,即与将drawable添加到布局组件相同。我只是想尽可能多地从代码和布局中分离样式,但这一直令人沮丧,所以我的结论是要么在样式中使用平面颜色,要么使用 android:backgroundTint 或将可绘制添加到 activity_main.xml appBarLayout .
  • 向视图添加背景属性确实没有错。除非您要使用单个样式添加多个属性,否则将 android:theme attr 与 android:background attr 放在视图上有什么区别?但是,如果您必须使用样式,只需将 android:theme 更改为 style,这样它只会影响 AppBarLayout 而不会影响其子项。
  • 是的,这很好。使用styles.xml 的魅力只是一种方便,将所有样式集中在一个地方,但布局中的样式仍然比Java 代码中的样式更可取。我不应该在深夜编码,太容易出错或陷入错误的想法,所以感谢您让我回到正确的道路上。
【解决方案2】:
"android:background" 

在您的样式中将背景更改为布局中的每个组件。 您可以使用类似这样的方式更改 ActionBar 的颜色。

<item name="android:navigationBarColor">@color/colorPrimary</item>

【讨论】:

  • 这会设置颜色,但我想将背景设置为图像。
【解决方案3】:

这是AppCompat-v7 Toolbar 的示例,另一个Toolbar 在主题方面有点不同。

我真的不能给你最适合你的答案,这段代码只是为了给你指路。

变化是工具栏使用style而不是android:theme,并且使用了新样式AppTheme.Toolbar

AppTheme.Toolbar 将您的自定义背景、工具栏内视图的主题(标题、副标题等)和弹出菜单的主题分组。

子主题和弹出菜单是分开的,因此您可以单独控制每个组件。

尝试使用他们的parent 属性(从暗到亮)并选择最适合您的。

进入你的styles.xml

<style name="AppTheme.Toolbar">
    <item name="android:background">@drawable/actionbar_background</item>
    <item name="popupTheme">@style/AppTheme.PopupOverlay</item>
    <item name="android:theme">@style/AppTheme.AppBarOverlay</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- Change attribute of the toolbar views-->
</style>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
    <!--Change attribute of the menu-->
</style>

进入你的activity_main.xml

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        style="@style/AppTheme.Toolbar"
        android:layout_height="?attr/actionBarSize" />

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2020-02-10
    相关资源
    最近更新 更多