Android 横竖屏切换、虚拟键盘相关问题备忘

来源:互联网 发布:30岁的程序员 编辑:程序博客网 时间:2024/05/17 21:41

微笑Android默认的横竖屏切换时,Activity是重新加载的(会调用onDestroy,再重新onCreate)。如果不想Activity重新加载。可以这样:

1.  在AndroidManifest.xml中为Activity设置configChanges属性

<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.coffice.MainActivity"            android:configChanges="orientation|keyboardHidden|screenSize"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

注:网上很多文章说的是设置前两项,orientation|keyboardHidden。但是Android3.2以上只设置前两项不起作用。一定得加上screenSize。

2.  在对应的Activity中重写:onConfigurationChanged 方法:

@Overridepublic void onConfigurationChanged(Configuration newConfig) {// TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);if(orientation == Configuration.ORIENTATION_PORTRAIT) {//竖屏//竖屏操作}else if(orientation == Configuration.ORIENTATION_LANDSCAPE){//横屏//横屏操作}}

到此,当你横竖屏切换时,Activity将不会被重新加载。而只是调用Activity里的onConfigurationChanged方法。

微笑虚拟键盘相关问题

有些情况下,虚拟键盘弹出来后会挤压原有布局,用户体验极差。只需要这样:

AndroidManifest.xml中为Activity设置windowSoftInputMode属性

android:windowSoftInputMode="adjustPan"


windowSoftInputMode其他的一些属性说明:

stateHidden默认隐藏(当Activity展示时,即使当前焦点在某个EditText上也不会弹出虚拟键盘,

只有你再选择某个EditText时才弹出) 

stateVisible默认显示(当Activity展示时,若当前焦点是在某个EditText,则直接弹出虚拟键盘)