2015-05-23 129 views
1

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

我已经制作了一张我正在努力实现的图像,但是我的声望太低而无法上传图像,所以我尽量用文字来描述。如果我错过了任何相关信息,请告诉我。

回答

6

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

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

+0

哇。非常感谢你。 –