简体   繁体   中英

Android: How to programmatically make an Activity Window transluscent?

Is there a way to programmatically make an Activity Window appear transluscent? For my purposes, I cannot use static XML resources.

I attempted to set the background resource to a transluscent color, but that only make the background appear solid black.

write it in your activity class

Window window = this.getWindow();
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Finally, I find a way to solve this problem.

https://github.com/ikew0ng/SwipeBackLayout/blob/e4ddae6d2b8af9b606493cba36faef8beba94be2/library/src/main/java/me/imid/swipebacklayout/lib/Utils.java

if you want to make a activity translucent

Utils.convertActivityToTranslucent(activity);

or if you want to make it opaque

Utils.convertActivityFromTranslucent(activity);

A bit late, but for posterity's sake...you can set the translucent theme programmatically like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Set a Translucent NoTitleBar theme before calling super.onCreate()
    setTheme(android.R.style.Theme_Translucent_NoTitleBar);

    super.onCreate(savedInstanceState);
}

API 30 added a setTranslucent method for achieving this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    setTranslucent(true)
    window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM