当前位置: 代码迷 >> 综合 >> getWindow().setBackgroundDrawable(null);对UI优化的测试
  详细解决方案

getWindow().setBackgroundDrawable(null);对UI优化的测试

热度:28   发布时间:2024-01-11 23:01:58.0

今天工作不多,就测试了下 getWindow().setBackgroundDrawable(null);对UI优化的测试

OK首先我们用手机开发者模式自带的 OverDraw来判断布局的OverDraw状况,

随便写了一个默认布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="#fff"  >
<!--单纯就是个继承RelativeLayout的子类-->
<com.xxx.widget.CustomRelativeLayout android:layout_width="300dp" android:layout_height="400dp" android:background="#fff"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="按钮"/> </com.xxx.widget.CustomRelativeLayout></RelativeLayout>

显示如上,我们可以看到 即使是根布局RelativeLayout也是有蓝色的,当然, 蓝色是可以接受的,但是button就彻底的飘红了,啥情况,有优化的余地么

当然有,

我们在onCreate的 setContentView 后加上这行代码

setContentView(R.layout.activity_test);
getWindow().setBackgroundDrawable(null);
ps:window的默认毕竟色是白色,如果没了那就要在xml根布局自己把背景色白色加上,要不会有问题


我们可以看到, 这次根布局没有颜色了,蓝色移到了里面的RelativeLayout,,Button变成了绿色是我们想要的结果, 这样就避免了多一层overdraw了

  相关解决方案