android浮动透明对话框,Android中具有透明背景的对话框

这篇博客介绍了如何解决现有答案中对话框边距丢失的问题。作者通过研究Android SDK,发现并修改了默认的窗口背景drawable,使其支持透明对话框。主要步骤包括复制并修改dialog_background_material.xml文件,将颜色设置为透明,然后在主题中应用这个新的背景资源,从而创建出允许透明背景的AlertDialog。
摘要由CSDN通过智能技术生成

One issue I found with all the existing answers is that the margins aren't preserved. This is because they all override the android:windowBackground attribute, which is responsible for margins, with a solid color. However, I did some digging in the Android SDK and found the default window background drawable, and modified it a bit to allow transparent dialogs.

First, copy /platforms/android-22/data/res/drawable/dialog_background_material.xml to your project. Or, just copy these lines into a new file:

android:inset="16dp">

Notice that android:color is set to ?attr/colorBackground. This is the default solid grey/white you see. To allow the color defined in android:background in your custom style to be transparent and show the transparency, all we have to do is change ?attr/colorBackground to @android:color/transparent. Now it will look like this:

android:inset="16dp">

After that, go to your theme and add this:

@drawable/newly_created_background_name

@color/some_transparent_color

Make sure to replace newly_created_background_name with the actual name of the drawable file you just created, and replace some_transparent_color with the desired transparent background.

After that all we need to do is set the theme. Use this when creating the AlertDialog.Builder:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);

Then just build, create, and show the dialog as usual!

透明对话框 void TransparentBlt( HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HBITMAP hBitmap, int nXSrc, int nYSrc, COLORREF colorTransparent, HPALETTE hPal ) { CDC dc, memDC, maskDC, tempDC; dc.Attach( hdcDest ); maskDC.CreateCompatibleDC(&dc); CBitmap maskBitmap; //add these to store return of SelectObject() calls CBitmap* pOldMemBmp = NULL; CBitmap* pOldMaskBmp = NULL; HBITMAP hOldTempBmp = NULL; memDC.CreateCompatibleDC(&dc); tempDC.CreateCompatibleDC(&dc); CBitmap bmpImage; bmpImage.CreateCompatibleBitmap( &dc, nWidth, nHeight ); pOldMemBmp = memDC.SelectObject( &bmpImage ); // Select and realize the palette if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && hPal ) { ::SelectPalette( dc, hPal, FALSE ); dc.RealizePalette(); ::SelectPalette( memDC, hPal, FALSE ); } hOldTempBmp = (HBITMAP) ::SelectObject( tempDC.m_hDC, hBitmap ); memDC.BitBlt( 0,0,nWidth, nHeight, &tempDC, nXSrc, nYSrc, SRCCOPY ); // Create monochrome bitmap for the mask maskBitmap.CreateBitmap( nWidth, nHeight, 1, 1, NULL ); pOldMaskBmp = maskDC.SelectObject( &maskBitmap ); memDC.SetBkColor( colorTransparent ); // Create the mask from the memory DC maskDC.BitBlt( 0, 0, nWidth, nHeight, &memDC, 0, 0, SRCCOPY ); // Set the background in memDC to black. Using SRCPAINT with black // and any other color results in the other color, thus making // black the transparent color memDC.SetBkColor(RGB(0,0,0)); memDC.SetTextColor(RGB(255,255,255)); memDC.BitBlt(0, 0, nWidth, nHeight, &maskDC, 0, 0, SRCAND); // Set the foreground to black. See comment above. dc.SetBkColor(RGB(255,255,255)); dc.SetTextColor(RGB(0,0,0)); dc.BitBlt(nXDest, nYDest, nWidth, nHeight, &maskDC, 0, 0, SRCAND); // Combine the foreground with the background dc.BitBlt(nXDest, nYDest, nWidth, nHeight, &memDC, 0, 0, SRCPAINT); if (hOldTempBmp) ::SelectObject( tempDC.m_hDC, hOldTempBmp); if (pOldMaskBmp) maskDC.SelectObject( pOldMaskBmp ); if (pOldMemBmp) memDC.SelectObject( pOldMemBmp ); dc.Detach(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值