ActionBarSherlock - 带分隔线的actionbar自定义背景

31
我正在实现 ActionBarsherlock,并且我想更改操作栏的背景。
我重写了属性,但是蓝色分隔线消失了。如何在自定义背景中使用蓝色分隔线?
<style name="Theme.MyTheme" parent="Theme.Sherlock.ForceOverflow">
    <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
</style>

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">#ff3e3f3d</item>
    <item name="background">#ff3e3f3d</item>
</style>

4个回答

76

对于那些(像我一样)不擅长使用 9-patch 图像的人,你可以使用 XML 可绘制对象在操作栏底部获取分隔符:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Bottom Line -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/action_bar_line_color" />
        </shape>
    </item>

    <!-- Color of your action bar -->
    <item android:bottom="2dip">
        <shape android:shape="rectangle">
            <solid android:color="@color/action_bar_color" />
        </shape>
    </item>
</layer-list>

将此保存为 drawable/action_bar_background.xml,然后在您的主题中应用:

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">@drawable/action_bar_background</item>
    <item name="background">@drawable/action_bar_background</item>
</style>

寻找了一个星期才找到像这样的东西,真的很不错! - Johan S
如何将此应用于Theme.Sherlock.Light? - tobias
菜单项的分隔符怎么处理? - Roy Lee
我正在这里做类似的事情!!! https://dev59.com/7HjZa4cB1Zd3GeqPeHTt - Etienne Lawlor
你,我的朋友,真是太棒了。 - Sheharyar
此博客还展示了如何使用layer-list来创建更加华丽的ActionBar教程:http://krishnalalstha.wordpress.com/2013/09/24/make-your-action-bar-beautiful/ - Someone Somewhere

17

1
非常感谢这个,这正是我一直在寻找的,使主题制作变得更加容易了! - AndroidNoob
有没有使用教程?我不知道在哪里复制这些图像并设置它们。 - Archie.bpgc

7
蓝色分隔线默认为背景。这是一个9-patch可绘制对象,可以确保该线始终出现在操作栏底部,无论其高度如何。
对于您的情况,我建议从Android中复制默认背景.png文件,然后修改它们,使可扩展部分的9-patch成为您的目标背景颜色。这将填充操作栏以显示所需颜色,同时保持底部的蓝色边框。

谢谢,我将复制默认背景并进行修改。 - ikarius

3

关于DanielGrech提供的答案,我想进行更新。在操作栏底部使用自定义图片也是可行的。像这样:

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Bottom Line with custom graphic instead of a solid color-->
    <item android:drawable="@drawable/bar">
          <shape android:shape="rectangle"/>
    </item>

    <!-- Color of your action bar -->
    <item android:bottom="2dip">
        <shape android:shape="rectangle">
            <solid android:color="@color/action_bar_color" />
        </shape>
    </item>
</layer-list>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接