In android background image does not change when orientation does

江枫思渺然 提交于 2019-12-07 14:32:51

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5156000/in-android-background-image-does-not-change-when-orientation-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!