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

Cam*_*gny 5 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>
Run Code Online (Sandbox Code Playgroud)

Cam*_*gny 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>
Run Code Online (Sandbox Code Playgroud)

请注意该行:

<item name="android:windowBackground">@drawable/notification_panel</item>
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 你能分享9路图像文件吗? (2认同)

Jas*_*son 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>
Run Code Online (Sandbox Code Playgroud)