方案A: 修改所有Activity Theme windowIsTranslucent = true 属性

同样的配方同样的味道 替换所有所有Activity Theme 将window 改为不透明,背景颜色改为透明

运行后的效果图:

闪烁透底的问题是解决了,但是侧滑框架出现了侧滑后看不到底部内容,方案A失败;

方案B:动态设置Activity Theme

在当前App退到后台时替换Activity为非透明主题,在Activity恢复到前台被点击时替换为透明主题; 如何动态修改Activity Theme?

@Override

protected void onCreate(Bundle savedInstanceState) {

if (current_theme!= -1){

this.setTheme(current_theme);

}

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById(R.id.bt_theme).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

changeTheme(GREEN_THEME);

}

});

}

public void changeTheme(int index) {

switch (index) {

case DEFAULT_THEME:

current_theme = R.style.DefaultTheme;

break;

case GREEN_THEME:

current_theme = R.style.GreenTheme;

break;

case ORANGE_THEME:

current_theme = R.style.OrangeTheme;

break;

default:

break;

}

}

protected void reload() {

Intent intent = getIntent();

overridePendingTransition(0, 0);

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

finish();

overridePendingTransition(0, 0);

startActivity(intent);

}

其实设置主题必须在任何view创建之前,所以我们不可能在activity的onCreate之后来更改主题,如果一定要做,就只能调用setTheme(),然后调用recreate(),重新创建一个activity,并且销毁上一个activity; 所以这个方案并不可行,整个界面必须销毁重建。 已知的Android theme修改方式

  • AndroidManifest 设置Activity Theme

  • 在Activity setContentView执行前 调用setTheme

可以通过其他方式修改Activity windowIsTranslucent 属性吗?

方案B+:反射动态设置Activity windowIsTranslucent

查阅Activity源码,看一下他是如何变成透明的

/**

  • Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from

  • opaque to translucent following a call to {@link #convertFromTranslucent()}.

  • Calling this allows the Activity behind this one to be seen again. Once all such Activities

  • have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will

  • be called indicating that it is safe to make this activity translucent again. Until

  • {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image

  • behind the frontmost Activity will be indeterminate.

  • This call has no effect on non-translucent activities or on activities with the

  • {@link android.R.attr#windowIsFloating} attribute.

  • @param callback the method to call when all visible Activities behind this one have been

  • drawn and it is safe to make this Activity translucent again.

  • @param options activity options delivered to the activity below this one. The options

  • are retrieved using {@link #getActivityOptions}.

  • @return true if Window was opaque and will become translucent or

  • false if window was translucent and no change needed to be made.

  • @see #convertFromTranslucent()

  • @see TranslucentConversionListener

  • @hide

*/

@SystemApi

public boolean convertToTranslucent(TranslucentConversionListener callback,

ActivityOptions options) {

boolean drawComplete;

try {

mTranslucentCallback = callback;

mChangeCanvasToTranslucent = ActivityManager.getService().convertToTranslucent(

mToken, options == null ? null : options.toBundle());

WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);

drawComplete = true;

} catch (RemoteException e) {

// Make callback return as though it timed out.

mChangeCanvasToTranslucent = false;

drawComplete = false;

}

if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {

// Window is already translucent.

mTranslucentCallback.onTranslucentConversionComplete(drawComplete);

}

return mChangeCanvasToTranslucent;

}

/**

  • Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a

  • fullscreen opaque Activity.

  • Call this whenever the background of a translucent Activity has changed to become opaque.

  • Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.

  • This call has no effect on non-translucent activities or on activities with the

  • {@link android.R.attr#windowIsFloating} attribute.

  • @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,

  • ActivityOptions)

  • @see TranslucentConversionListener

  • @hide

*/

@SystemApi

public void convertFromTranslucent() {

try {

mTranslucentCallback = null;

if (ActivityManager.getService().convertFromTranslucent(mToken)) {

WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

img

上述高清技术脑图以及配套的架构技术PDF可以点击我的GitHub免费获取

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

  • 无论你现在水平怎么样一定要 持续学习 没有鸡汤,别人看起来的毫不费力,其实费了很大力,这四个字就是我的建议!!!
  • 我希望每一个努力生活的IT工程师,都会得到自己想要的,因为我们很辛苦,我们应得的。

当我们在抱怨环境,抱怨怀才不遇的时候,没有别的原因,一定是你做的还不够好!

点击我的GitHub免费获取

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

  • 无论你现在水平怎么样一定要 持续学习 没有鸡汤,别人看起来的毫不费力,其实费了很大力,这四个字就是我的建议!!!
  • 我希望每一个努力生活的IT工程师,都会得到自己想要的,因为我们很辛苦,我们应得的。

当我们在抱怨环境,抱怨怀才不遇的时候,没有别的原因,一定是你做的还不够好!

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐