带圆边的 ActionBar

ActionBar with rounded edges

我想知道,是否可以让我的 ActionBar 具有圆角边缘,更具体地说,只有顶部的边缘(左上角、右上角)。我做了一些搜索,但大多数方法都过时了,对我不起作用。我正在使用 AppCompat 支持库 v22.1.1.

我已经制作了我想要达到的目标的图像,但我的声誉太低无法上传图像,所以我尽量用文字描述。如果我遗漏了任何相关信息,请告诉我。

是的,可以使用可绘制的形状。

先创建actionbar_foreground.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="#2C5AA9"/>
    <corners
        android:radius="0.1dp"
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>

</shape>

然后是 ActionBar 的背景。在边缘变圆的地方可以看到这种颜色。我在使用浅色主题时遇到了麻烦,装饰视图是白色的,所以我做了这个 "hack"

actionbar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#FF000000"/>
</shape>

现在您只需将它们分层(将一个放在另一个上面)。底层是背景。这就是我所说的actionbar_layer_list.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/actionbar_background"/>
    <item android:drawable="@drawable/actionbar_foreground"/>

</layer-list>

现在只需在 styles.xml

中为您的应用定义样式
 <!-- Base application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="CustomActionBarTheme" parent="@style/AppTheme">
        <item name="actionBarStyle">@style/CustomActionBar</item>
    </style>


    <!-- ActionBar styles -->
    <style name="CustomActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/actionbar_layer_list</item>
        <item name="titleTextStyle">@style/CustomActionBarTextStyle</item>
    </style>

并将其应用于AndroidManifest.xml

看起来像这样