本次实验 是在 android 7.0 模拟器下完成的,可能和真机有差别
1.首先 横竖屏 转换时, 布局文件 是如何 转换的,能否在 Activity 下 实现 不同名布局,
2..同名布局 的横竖屏自动识别?
3.用 orientation 和 orientation/keyhidden 对 oncreate 和 onconfigchaged 的区别
过程
1.在 判断横竖屏 的过程中 使用 一下两种 不同名的布局
public void HenorShu(){
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
//this.setContentView(view)
this.setContentView(R.layout.main);
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
this.setContentView(R.layout.nain);
}
}
结果可以显示 main 和 nain 布局
2.将 上面布局 的 main,改为nain,在 layout 文件夹下 写横竖屏的不同布局
结果能自动识别
3.
oritation
每次转屏都是要重启的 oncreate 函数
但是不是每次横竖屏转换都要调用
onconfigchanged 这个函数 大概要4次转换才可以有一次调用
orientation|keyboardHidden
不会再调用 concreate()函数了
竖屏转 横屏 会调用一次onconfigchanged
横屏转竖屏 会调用 两次onconfigchanged
总结分析:
公司 的 泡泡布局,现在试了在 orientation/keyhidden 的情况下,不重载数据,在 onconfig 里面去 重新 设置布局,结果 页面显示,但是很多数据都没读出来
可能是 光重新读入了 xml的布局,但是好多 实力话过程,没有像页面切换时 实现
解决方案
1. 重新读入,然后去实例化
2.orientation 的方式,先保存,那个页面的实力对象,去oncreate 一下
代码
Activity xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zgd"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HengShuPActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
三个布局文件
main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am main"
/>
</LinearLayout>
nain
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello shu pingh"
/>
</LinearLayout>
nian2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello shu pingh"
/>
</LinearLayout>
java 代码
package com.zgd;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
public class HengShuPActivity extends Activity {
private static int i=1;
private static int m=1;
/** Called when the activity is first created. */
private final int Hen=1;
private final int Shu=2;
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.i("ConfigurationChanged","Yes "+i++);
HenorShu();
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("CongQi","yes "+m++);
super.onCreate(savedInstanceState);
setContentView(R.layout.nain);
HenorShu();
}
public void HenorShu(){
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
//this.setContentView(view)
this.setContentView(R.layout.main);
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
this.setContentView(R.layout.nain);
}
}
}