在我的应用程序中,我有一个图像作为背景。当应用程序处于纵向模式时,这很好。但是当它处于横向模式时,图像会转动并被拉伸。
我知道我们可以使用layout-land文件夹,但是使用这个解决方案,我的所有布局都只是一个图像(不太合乎逻辑..)。
还有其他解决方案吗?
PS:我的图像只是一个碳纹理,因此,防止它随着方向转动的解决方案就可以了。
编辑:
hotveryspicy,你的解决方案有效,但是有些活动有 android:configChanges =“orientation”(在Manifest中),它对它们不起作用。怎么解决这个问题?
事实上,当方向改变时,我使用此属性来保存状态,但这不是这样做的好方法。我需要使用onSaveInstanceState。
与此同时,是否有一个解决方案可以让我保持这个糟糕的解决方案?
答案 0 :(得分:2)
ya,首先检查 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
}
答案 1 :(得分:1)
如果将备用版本的背景图像放入drawable-land
res文件夹,它会不会有效?这样,您仍然可以使用相同的布局文件,并参考其中的@drawable/background
。
答案 2 :(得分:0)
我怀疑你可以做你所描述的事情。唯一可行的解决方案(布局 - 土地)你已经排除了。这是我猜的唯一方法。