歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 橫豎屏和布局問題

在這裡我做了一個小test,之前看過關於這方面的東西,到後來我才真正的遇到項目的時候,不得已才要去真正的熟悉裡面的流程。

這裡我貼測試源碼:

TestActivity.java

package cn.com.hrmdemo;

import Android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle arg0) {
System.out.println("------ TestActivity onCreate------");
Toast.makeText(this, "onCreate。。。", Toast.LENGTH_SHORT).show();
super.onCreate(arg0);
setContentView(R.layout.test_linearlayout);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
System.out.println("------ TestActivity onRestoreInstanceState------");
Toast.makeText(this, "onRestoreInstanceState。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onStart() {
System.out.println("------ TestActivity onStart------");
Toast.makeText(this, "onStart。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onStart();
}

@Override
protected void onRestart() {
System.out.println("------ TestActivity onRestart------");
Toast.makeText(this, "onRestart。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onRestart();
}

@Override
protected void onResume() {
System.out.println("------ TestActivity onResume------");
Toast.makeText(this, "onResume。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onResume();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
System.out.println("------ TestActivity onSaveInstanceState------");
Toast.makeText(this, "onSaveInstanceState。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}

@Override
protected void onPause() {
System.out.println("------ TestActivity onPause------");
Toast.makeText(this, "onPause。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onPause();
}

@Override
protected void onStop() {
System.out.println("------ TestActivity onStop------");
Toast.makeText(this, "onStop。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onStop();
}

@Override
protected void onDestroy() {
System.out.println("------ TestActivity onDestroy------");
Toast.makeText(this, "onDestroy。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onDestroy();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
System.out.println("------ TestActivity onConfigurationChanged------");
Toast.makeText(this, "屏幕轉變了。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}

}

------------------------------------------------------上面是源碼----------------------------------------------------------

下面看看demo的配置和運行的效果是怎樣

這個橫豎屏的配置主要是在AndroidManiFest.xml,就是Android App 的清單文件:

<activity .....

android:configChanges="想要配置的方式"

>

在你的activity 裡面配置你的橫豎屏切換方式

1). 什麼都不做配置的情況下

a.正常情況下:

07-24 08:43:59.519: I/System.out(18726): ------ TestActivity onCreate------
07-24 08:43:59.609: I/System.out(18726): ------ TestActivity onStart------
07-24 08:43:59.619: I/System.out(18726): ------ TestActivity onResume------


b.切換橫屏情況下:

07-24 08:51:01.359: I/System.out(18936): ------ TestActivity onPause------
07-24 08:51:01.379: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:51:01.389: I/System.out(18936): ------ TestActivity onStop------
07-24 08:51:01.399: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:51:01.429: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5bb4d000 size:4608000 offset:3072000
07-24 08:51:01.429: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c8c5000 size:16531456 offset:14995456
07-24 08:51:01.449: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:51:01.579: I/System.out(18936): ------ TestActivity onStart------
07-24 08:51:01.579: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:51:01.599: I/System.out(18936): ------ TestActivity onResume------

c.再切換回豎屏的情況下:

07-24 08:51:42.669: I/System.out(18936): ------ TestActivity onPause------
07-24 08:51:42.689: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:51:42.699: I/System.out(18936): ------ TestActivity onStop------
07-24 08:51:42.729: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:51:42.759: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c8c5000 size:4608000 offset:3072000
07-24 08:51:42.759: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5cd2a000 size:10797056 offset:9261056
07-24 08:51:42.789: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:51:42.859: I/System.out(18936): ------ TestActivity onStart------
07-24 08:51:42.859: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:51:42.879: I/System.out(18936): ------ TestActivity onResume------

2)配置android:configChanges="orientation|keyboardHidden" (4.0以前)

a.正常情況下:

07-24 08:54:05.059: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:54:05.199: I/System.out(18936): ------ TestActivity onStart------
07-24 08:54:05.219: I/System.out(18936): ------ TestActivity onResume------

b.切換橫屏情況下:

07-24 08:54:25.909: I/System.out(18936): ------ TestActivity onPause------
07-24 08:54:25.919: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:54:25.929: I/System.out(18936): ------ TestActivity onStop------
07-24 08:54:25.949: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:54:25.979: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c7c5000 size:19603456 offset:18067456
07-24 08:54:25.979: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5da77000 size:4608000 offset:3072000
07-24 08:54:26.019: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:54:26.069: I/System.out(18936): ------ TestActivity onStart------
07-24 08:54:26.079: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:54:26.079: I/System.out(18936): ------ TestActivity onResume------

c.再切換回豎屏的情況下:

07-24 08:55:02.099: I/System.out(18936): ------ TestActivity onPause------
07-24 08:55:02.109: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:55:02.119: I/System.out(18936): ------ TestActivity onStop------
07-24 08:55:02.129: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:55:02.139: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c7c5000 size:4608000 offset:3072000
07-24 08:55:02.139: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5cc30000 size:9216000 offset:7680000
07-24 08:55:02.179: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:55:02.249: I/System.out(18936): ------ TestActivity onStart------
07-24 08:55:02.289: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:55:02.309: I/System.out(18936): ------ TestActivity onResume------

發覺上面的結果是跟網上的朋友說的不是一樣的,原來是有區別的。

到後來去網上搜索了一下資料發覺,到了。4.0以後。要加上這樣的配置才會使Activity的生命周期不會重新被調用

3)配置android:configChanges="keyboardHidden|orientation|screenSize" (4.0以後)

a.正常情況下:

07-24 08:54:05.059: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:54:05.199: I/System.out(18936): ------ TestActivity onStart------
07-24 08:54:05.219: I/System.out(18936): ------ TestActivity onResume------


b.切換橫屏情況下:

07-24 09:08:21.639: I/System.out(19533): ------ TestActivity onConfigurationChanged------

c.再切換回豎屏的情況下:

07-24 09:08:21.639: I/System.out(19533): ------ TestActivity onConfigurationChanged-----

總結:要想Activity在橫豎屏切換的時候不重新加載生命周期的重要方法,一定要加上配置

4.0之前

<activity .....

android:configChanges="orientation|keyboardHidden"

>

4.0之後

<activity .....

android:configChanges="keyboardHidden|orientation|screenSize"

>

------------------------------------------------------常用設置---------------------------------------------------------

1.如果你的app指定要橫屏顯示的話

<activity .....

android:screenOrientation="portrait"

>

2.如果你的app指定要豎屏顯示的話

<activity .....

android:screenOrientation="landscape"

>

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm

Ubuntu 12.04搭建Android開發環境 http://www.linuxidc.com/Linux/2012-09/69961.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved