带圆边的ActionBar

Mat*_*nik 3 android android-appcompat android-actionbar

我想知道,如果可以使我的ActionBar具有圆形边缘,更具体地说只有顶部边缘(左上角,右上角).我做了一些搜索,但大多数方法已经过时,并不适合我.我正在使用AppCompat支持库v22.1.1.

我已经描绘了我想要实现的目标,但我的声誉太低,无法上传图像,所以我试着尽可能用语言来描述.如果我错过了任何相关信息,请告诉我.

Boj*_*man 10

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

首先创建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>
Run Code Online (Sandbox Code Playgroud)

然后是ActionBar的背景.将看到边缘为圆形的颜色.因为我使用的是浅色主题,装饰视图是白色的,所以我做了这个"黑客"

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>
Run Code Online (Sandbox Code Playgroud)

现在你只需将它们分层(将一个放在另一个上).底层是背景.这就是我所说的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>
Run Code Online (Sandbox Code Playgroud)

现在只需在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>
Run Code Online (Sandbox Code Playgroud)

并将其应用于AndroidManifest.xml

它看起来像这样

在此输入图像描述