在Android的背景下

在Android的背景下

本文介绍了在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">

和我有2个可绘制背景设在绘制和绘土地的文件夹。
当方向改变一切正常,但根据定向背景没有改变。它始终保持最先绘制背景。

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.

我试着在 onConfigurationChanged 手动更改方法通过添加一行:

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.

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

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>

我改变AndroidManifest.xml中的

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 ?

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

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

推荐答案

而不是在方向变化切换绘制,而是可以切换布局。通过将具有相同名称的XML文件中的布局土地/ 文件夹,当屏幕方向为横向操作系统将加载此替代布局。这样一来,你永远是使用正确的绘制(因为它们可以分别在两个XML文件的不同规定),并有额外的好处,可以独立优化布局为两个方向!

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.

这篇关于在Android的背景下,当方向不图像不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:34