改变方向时不要改变背景

发布于 2024-12-05 00:52:15 字数 452 浏览 0 评论 0原文

在我的应用程序中,我有一个图像作为背景。当应用程序处于纵向模式时,这很好。但当处于横向模式时,图像会旋转并被拉伸。

我知道我们可以使用layout-land文件夹,但是使用这个解决方案,我的所有布局都将是双精度的,仅用于图像(不太合乎逻辑......)。

还有其他解决方案吗?

PS:我的图像只是碳纹理,因此,防止其随方向转动的解决方案就可以了。

编辑:

hotveryspicy,您的解决方案有效,但是,有些活动具有属性 android:configChanges="orientation" (在清单中),并且它对它们不起作用。怎么解决这个问题呢?

事实上,我使用此属性在方向更改时保存状态,但这不是这样做的好方法。我需要为此使用 onSaveInstanceState 。

与此同时,是否有一个解决方案可以让我保留这个糟糕的解决方案?

In my application, I have an image as background. When the application is in portrait mode, that's fine. But when it is in landscape mode, the image turns and is stretched.

I know that we can use the layout-land folder, but with this solution, all my layouts will be in double just for an image (not very logical..).

Is there another solution?

PS : My image is just a carbon texture so, a solution to prevent it to turn with orientation would be ok.

EDIT :

hotveryspicy, your solution works BUT, there are activities which have the property android:configChanges="orientation" (in the Manifest), and it doesn't work for them. How to solve this?

In fact, I use this property to save state when the orientation changes but it's not the good way to do that. I need to use onSaveInstanceState for that.

In the meantime, is there a solution which allows me to keep this bad solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光匆匆的小流年 2024-12-12 00:52:15

是的,首先检查 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
}

ya, first check orientation in 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
}
后来的我们 2024-12-12 00:52:15

如果将背景图像的替代版本放入 drawable-land res 文件夹中,是否可以解决问题?这样您仍然可以使用相同的布局文件并引用其中的@drawable/background

Would it do the trick if you placed alternate version of the background image into drawable-land res folder? This way you can still use the same layout files and refer to the @drawable/background in them.

九厘米的零° 2024-12-12 00:52:15

我怀疑是否可以做你所描述的事情。您已经排除了唯一可能的解决方案(布局土地)。我想这是唯一的方法。

I doubt it would be possible to do what you had described. The only solution which was possible (layout-land) you had already ruled out. It is the only way to go about it I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文