如何圆角添加到活动的半透明背景是什么? [英] How to add rounded corners to Translucent background of activity?

查看:146
本文介绍了如何圆角添加到活动的半透明背景是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的活动,我想有一个圆角矩形。活动采用了半透明的绘制对象。我所看到的弹出窗口由是半透明的其他开发人员(而不是对话的主题)的圆角,我试图复制的。任何帮助将是AP preciated。

下面是code我现在有能产生矩形半透明窗口在屏幕的中间

 <样式名称=Theme.TranslucentDialog>
    <项目名称=机器人:colorBackgroundCacheHint> @空< /项目>
    <项目名称=机器人:windowIsTranslucent>真< /项目>
    <! - 请注意,我们使用基本动画风格在这里(即无
         动画),因为我们真的不知道如何样
         活动将被使用。 - >
    <项目名称=机器人:windowBackground> @可绘制/ translucent_background< /项目>
    <项目名称=机器人:windowNoTitle>真< /项目>
    <项目名称=机器人:colorForeground>#FFF< /项目>
    <项目名称=机器人:windowIsFloating>真< /项目>
    <项目名称=机器人:backgroundDimEnabled>假< /项目>
    <项目名称=机器人:windowAnimationStyle> @android:款式/ Animation.Translucent< /项目>
< /风格>

<绘制NAME =translucent_background>#6000< /绘制>
 

解决方案

我是不是能够通过生成在code自定形状来做到这一点。我不得不创建我自己的9补丁png文件来实现这一点。下面是我创建的最终主题:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=主题父=安卓主题>
    < /风格>

    <样式名称=Theme.TranslucentDialog>
        <项目名称=机器人:colorBackgroundCacheHint> @空< /项目>
        <项目名称=机器人:windowIsTranslucent>真< /项目>
        <项目名称=机器人:windowNoTitle>真< /项目>
        <项目名称=机器人:colorForeground>#FFFFFF< /项目>
        <项目名称=机器人:windowIsFloating>真< /项目>
        <项目名称=机器人:backgroundDimEnabled>假< /项目>
        <项目名称=机器人:windowAnimationStyle> @android:款式/ Animation.Translucent< /项目>
        <项目名称=机器人:windowBackground> @可绘制/ notification_panel< /项目>
    < /风格>

< /资源>
 

请注意行:

 <项目名称=机器人:windowBackground> @可绘制/ notification_panel< /项目>
 

这是设置活动给我创造了9片图像的背景就行了。此图片可以是任何东西,图像,9补丁或自定形状。我用了9补丁,这样我可以有一个很好的边框和圆角,同时保持一个非常透明的活动窗口(显示它背后的一切,但留下的灰色色调不错的地方活动窗口的位置)。

在9贴片notification_panel.9.png位于我绘制的文件夹。起初我有点害怕创建自己的9个补丁的形象,但事实证明,使用的Inkscape和Android draw9patch.bat应用程序,我能有令人满意的结果做到这一点。

让我知道如果任何人有任何的问题。

I have a simple Activity and I would like to have the a rounded rectangle shape. The activity uses a translucent Drawable. I have seen popup windows by other developers that are translucent (not a dialog theme) with rounded corners and I am trying to replicate that. Any help would be appreciated.

Here is the code I currently have that produces a rectangular translucent window in the middle of the screen.

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

解决方案

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.

这篇关于如何圆角添加到活动的半透明背景是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆