在android中,当方向改变时,背景图像不会改变

发布于 2024-10-19 12:06:55 字数 1555 浏览 5 评论 0原文

我的 ActivityGroup 有一个定义的线性布局

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata"
  android:id="@+id/homeActivityGroupBG"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="@drawable/background">

,并且我有 2 个背景可绘制对象位于“drawable”和“drawable-land”文件夹中。 当方向改变时,一切正常,但背景不会根据方向改变。它总是保留第一个可绘制的背景。

我尝试通过添加以下行在 onConfigurationChanged 方法中手动更改它:

background.setBackgroundResource(R.drawable.background);

它解决了问题。但每次配置更改或传递活动时,它都会导致大量内存泄漏。

编辑:我创建一个 theme.xml 来定义窗口的背景图像。 XML 文件包含:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="android:windowBackground">@drawable/background</item>
</style>
</resources>

我更改了 AndroidManifest.xml,

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true" android:name="com.matriksdata.app.DefaultApplication"
android:theme="@style/Theme">

并从布局中删除了背景元素。根本没有任何改变。当设备方向发生变化时,我无法获得新的可绘制对象,并且出现内存泄漏,最终导致应用程序崩溃。

当方向改变时,还有其他方法可以强制改变可绘制对象吗?或者应用程序是否有任何原因导致内存泄漏?

编辑:对于内存泄漏问题,我在 Android 内存使用问题提出了一个问题关于可能使用 ActivityGroup

I have a linear layout for my ActivityGroup as defined

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata"
  android:id="@+id/homeActivityGroupBG"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="@drawable/background">

And i have 2 background drawables located in "drawable" and "drawable-land" folders.
When orientation changes everything is normal but background is not changed according to orientation. It always keeps the very first drawable for background.

I tried to change it manually in onConfigurationChanged method by adding the line:

background.setBackgroundResource(R.drawable.background);

it solves the problem. But it causes huge amount of memory leaks every time configuration changes or passing through activities.

Edit: I create a theme.xml to define background image to window. XML file contains:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="android:windowBackground">@drawable/background</item>
</style>
</resources>

I changed AndroidManifest.xml as

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true" android:name="com.matriksdata.app.DefaultApplication"
android:theme="@style/Theme">

and i removed background elements from my layouts. Simply nothing has changed. I can not get new drawable when device orientation changes and i get memory-leaks that causes application crashes eventually.

Is there any other way to force to change drawables when orientation does? Or is there any reason that the application makes memory-leaks ?

Edit : for memory leak problem i had asked a question at Android Memory Usage Problem on possibly using ActivityGroup

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

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

发布评论

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

评论(1

要走干脆点 2024-10-26 12:06:55

您可以切换布局,而不是在方向改变时切换可绘制对象。通过将同名的 xml 文件放入 layout-land/ 文件夹中,操作系统将在屏幕方向为横向时加载此备用布局。这样,您将始终使用正确的可绘制对象(因为它们可以在两个 xml 文件中以不同的方式指定),并且还有一个额外的优点,即您可以独立优化两个方向的布局!

Android 开发者博客上的这篇文章提出了一些建议关于如何在方向改变时保留对象并避免过程中内存泄漏的提示和技巧;这将对您有进一步的帮助。

Rather than switch the drawable upon orientation change, you can instead switch the layout. By placing an xml file of the same name in the layout-land/ folder, the OS will load this alternate layout when the screen orientation is landscape. That way, you will always be using the correct drawable (as they can be specified differently in each of the two xml files), and there is the added advantage that you can independently optimize your layout for both orientations!

This post on the Android Developer's blog suggests some tips and tricks on how to retain objects upon orientation change and avoid memory leaks in the process; which will be of further assistance to you.

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