How to add rounded corners to Translucent background of activity?

。_饼干妹妹 提交于 2019-12-04 09:47:58

I was not able to do this by generating a custom shape in the code. I had to accomplish this by creating my own 9-Patch png file. Here is the final Theme that I created:

<?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>

Please note the line:

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

This is the line that sets the background of the activity to the 9-Patch image that I created. This image could be anything, an image, a 9-patch or a custom shape. I used a 9-Patch so that I could have a nice border and rounded corners while retaining a very translucent activity window (showing everything behind it but leaving a nice hue of grey where the activity window is located).

The 9-patch notification_panel.9.png is located in my drawable folder. At first I was a little bit intimidated to create my own 9-patch image, but it turns out that by using Inkscape and the android draw9patch.bat utility program, I was able to do this with satisfactory results.

Let me know if anyone has any questions.

You need to make a custom shape. Here's an example xml file (white rectangle with rounded corners):

<?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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!