带圆边的ActionBar

时间:2015-05-23 20:17:00

标签: android android-actionbar appcompat-v7-r22.1

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

我已经对我想要实现的目标进行了描述,但我的声誉太低而无法上传图像,因此我尝试尽可能用语言来描述。如果我错过了任何相关信息,请告诉我。

1 个答案:

答案 0 :(得分:8)

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

首先创建 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的背景。将看到边缘为圆形的颜色。因为我使用的是浅色主题,装饰视图是白色的所以我遇到了麻烦,所以我做了这个“黑客”

<强> 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

它看起来像这样

enter image description here

相关问题