android 设置窗口透明效果,android - 如何将对话框窗口背景设置为透明,而不影响其边距...
當(dāng)前,我有以下對話框,我將對其項(xiàng)目執(zhí)行擴(kuò)展/折疊動畫。
該對話框是通過以下代碼創(chuàng)建的import android.support.v7.app.AlertDialog;
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
ViewTreeObserver obs = view.getViewTreeObserver();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
// http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha
int width = dialog.getWindow().getDecorView().getWidth();
int height = dialog.getWindow().getDecorView().getHeight();
dialog.getWindow().setLayout(width, height);
}
});
但是,在執(zhí)行動畫時,這是副作用。
請注意,動畫后對話框中多余的多余白色區(qū)域不是由我們的自定義 View 引起的。它是對話框本身的系統(tǒng)窗口白色背景。
我傾向于使對話框的系統(tǒng)窗口背景變得透明。
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
盡管不再看到不需要的白色背景,但對話框的原始邊距也消失了。 (對話框的寬度現(xiàn)在是全屏寬度)
如何在不影響利潤的情況下使其透明?
最佳答案
有一個很簡單的方法可以做到這一點(diǎn):
您需要“修改”用作Drawable背景的Dialog。這些Dialogs使用InsetDrawable作為背景。
API> = 23
只有SDK API 23+允許您獲取由Drawable包裝的源InsetDrawable(getDrawable()方法)。有了這個,您可以做任何您想做的事-例如將顏色更改為完全不同的顏色(例如RED或其他顏色)。如果使用這種方法,請記住,包裝的Drawable是GradientDrawable而不是ColorDrawable!
API <23
對于較低的API,您的(“優(yōu)雅”)選項(xiàng)非常有限。
幸運(yùn)的是,您無需將顏色更改為一些瘋狂的值,只需將其更改為TRANSPARENT即可。為此,您可以在setAlpha(...)上使用InsetDrawable方法。InsetDrawable background =
(InsetDrawable) dialog.getWindow().getDecorView().getBackground();
background.setAlpha(0);
編輯(作為Cheok Yan Cheng's注釋的結(jié)果):
或者您實(shí)際上可以跳過轉(zhuǎn)換為InsetDrawable并獲得相同的結(jié)果。請記住,這樣做會導(dǎo)致alpha在InsetDrawable本身而不是Drawable包裹的InsetDrawable上更改。
總結(jié)
以上是生活随笔為你收集整理的android 设置窗口透明效果,android - 如何将对话框窗口背景设置为透明,而不影响其边距...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android finish()传参数,
- 下一篇: Android设置布局位置五等分,五等分