如何在Activity的半透明背景上添加圆角?

5

我有一个简单的Activity,希望它能呈现为圆角矩形。该Activity使用半透明的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个回答

10

我无法通过在代码中生成自定义形状来实现这一点。我不得不通过创建自己的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-patch图像或自定义形状。我使用了9-Patch以便在保留非常半透明的活动窗口(显示其后面的所有内容,但在活动窗口所在位置留下漂亮的灰色调)的同时具有漂亮的边框和圆角。

9-patch通知面板notification_panel.9.png位于我的drawable文件夹中。起初我有点害怕创建自己的9-patch图像,但后来我发现通过使用Inkscape和android draw9patch.bat实用程序,我能够得到令人满意的结果。

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


2
你能分享一下9路径图像文件吗? - Derzu
我不知道如何将png文件附加到帖子中 :( 如果你在谷歌上搜索“Android 9 Patch”,我相信你可以找到一些例子。 - Camille Sévigny

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>

你确定可以使用半透明的可绘制对象来完成这个任务吗?<drawable name="translucent_background">#60000000</drawable> - Camille Sévigny
只需将 startColorendColor 替换为 #60000000 - Jason Robinson
谢谢,我会尝试并告诉你结果如何。 - Camille Sévigny
抱歉,这对我没用。:( 如果您有完整的或可运行的代码集,我很想看看。 - Camille Sévigny
什么没有起作用?你是否已经用上面的新xml drawable替换了@drawable/translucent_background?你能否详细说明一下你看到了什么? - Jason Robinson
它只显示了一个普通的矩形,没有圆角。 - Camille Sévigny

-1

虽然这个问题已经快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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接