3

我的 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">

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

我尝试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的问题

4

1 回答 1

2

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.

于 2011-03-02T15:01:21.783 回答