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.再切换回竖屏的情况下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8b58ce77014cb1b899b61ac6a2a997c1.html