带圆角的ActionBar [英] ActionBar with rounded edges

查看:149
本文介绍了带圆角的ActionBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to know, if it is possible to make my ActionBar have rounded edges, more specifically only the top ones (top-left, top-right). I did some searching, but most of the methods are outdated and did not work for me. I am using AppCompat support library v22.1.1.

我已经为要实现的目标制作了一个图像,但是我的声誉太低而无法上传图像,因此我尝试用文字来尽可能地描述自己.如果我错过了任何相关信息,请告诉我.

I have made an image of what I am trying to achieve, but my reputation is too low to upload images so I tried to describe as best as I could with words. If I have missed out any relevant information, let me know.

推荐答案

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

Yes, it is possible by using a shape drawable.

首先创建 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"

And then the background for the ActionBar. This color will be seen where the edges are rounded. I had trouble since I was using a light theme, where the decor view is white in color, so I made this "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

And now you just layer them (put one over the other). Bottom layer is the background. This is what I called 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中为您的应用程序定义样式

And now just define a style for your app in the 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

看起来像这样

这篇关于带圆角的ActionBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆