如何在半透明活动背景中添加圆角?

时间:2011-05-23 18:58:58

标签: android background rounded-corners

我有一个简单的活动,我希望有一个圆角的矩形形状。该活动使用半透明的Drawable。我看到其他开发人员的弹出窗口是半透明的(不是对话框主题),有圆角,我试图复制它。任何帮助将不胜感激。

以下是我目前在屏幕中间生成矩形半透明窗口的代码。

<style name="Theme.TranslucentDialog">
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <!-- Note that we use the base animation style here (that is no
         animations) because we really have no idea how this kind of
         activity will be used. -->
    <item name="android:windowBackground">@drawable/translucent_background</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorForeground">#fff</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> 
</style>

<drawable name="translucent_background">#60000000</drawable>

3 个答案:

答案 0 :(得分:9)

我无法通过在代码中生成自定义形状来实现此目的。我必须通过创建自己的9-Patch png文件来实现这一目标。这是我创建的最终主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
    </style>

    <style name="Theme.TranslucentDialog">
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorForeground">#ffffff</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> 
        <item name="android:windowBackground">@drawable/notification_panel</item>
    </style>

</resources>

请注意以下内容:

<item name="android:windowBackground">@drawable/notification_panel</item>

这是将活动背景设置为我创建的9-Patch图像的行。此图像可以是任何图像,图像,9补丁或自定义形状。我使用了一个9-Patch,这样我就可以拥有一个漂亮的边框和圆角,同时保留一个非常半透明的活动窗口(显示它背后的所有内容,但在活动窗口所在的位置留下一个漂亮的灰色调)。

9-patch notification_panel.9.png位于我的drawable文件夹中。起初我有点害怕创建我自己的9补丁图像,但事实证明,通过使用Inkscape和android draw9patch.bat实用程序,我能够做到这一点,结果令人满意。

如果有人有任何问题,请告诉我。

答案 1 :(得分:5)

您需要制作自定义形状。这是一个示例xml文件(带圆角的白色矩形):

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#ffffff"
        android:endColor="#ffffff"/>
    <corners
        android:radius="8dp"/>
</shape>

答案 2 :(得分:0)

尽管这个问题已经有将近10年的历史了,但是要实现这个目标,您必须在drawable文件夹中创建一个xml文件。在此文件中定义如下形状:

 <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#0f000000"
        android:endColor="#0f000000"/>
    <corners
        android:radius="10dp"/>
</shape>

在Style.xml中创建一个新样式,并在其中创建背景项并定义@ drawable / yourFileNameInDrawbaleFolder.xml

<style name="AppTheme.customTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="windowNoTitle">true</item>
        <item name="android:background">@drawable/dialogbg</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:radius">15dp</item>
    </style>
相关问题