Android解决分屏情况下背景拉伸问题
1.需要新建对应宽度的res资源目录,放入对应的背景图
2.在activity启动后,注册监听根背景size变化getViewTreeObserver().addOnGlobalLayoutListener(this),有变化时重新设置背景。
@Override
protected void onCreate(Bundle savedInstanceState) {
rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
private int contentViewWidth = 0;
@Override
public void onGlobalLayout() {
int mLayerWidth = rootView.getWidth();
if (contentViewWidth != mLayerWidth) {
contentViewWidth = mLayerWidth;
Log.d(TAG, "onGlobalLayout=" + mLayerWidth);
getWindow().setBackgroundDrawableResource(R.drawable.bg_theme_background);
}
}
@Override
protected void onDestroy(Bundle savedInstanceState) {
try {
rootView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
|