在我的应用程序中,我有一个图像作为背景。当应用程序处于纵向模式时,这很好。但当处于横向模式时,图像会旋转并被拉伸(stretch)。
我知道我们可以使用layout-land文件夹,但是使用这个解决方案,我的所有布局都将是双倍的,仅用于图像(不太合逻辑......)。
还有其他解决办法吗?
PS:我的图像只是碳纹理,因此,防止其随方向转动的解决方案就可以了。
编辑:
hotveryspicy,您的解决方案有效,但是,有些 Activity 具有属性 android:configChanges="orientation" (在 list 中),并且它对它们不起作用。怎么解决这个问题?
事实上,我使用此属性在方向更改时保存状态,但这不是一个好方法。我需要为此使用 onSaveInstanceState。
同时,有没有一个解决方案可以让我保留这个糟糕的解决方案?
最佳答案
是的,首先检查onCreate()中的方向
Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int height= mDisplay.getHeight();
if(width>height)
{
//Landscape
//set landscape compatible image
}
else
{
//Portrait
//set portrait compatible image
}
关于android - 改变方向时不要改变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7429595/