横竖屏切换问题
- App 横竖屏切换的时候会销毁当前的 Activity 然后重新创建一个
- 横竖屏切换时 Activity 生命周期:onPause-> onStop-> onDestory-> onCreate->onStart->onResume
- 现在设置一个按钮和一个 TextView 显示文本,点击按钮后,修改 TextView 默认的文本,然后横竖屏切换,会发现 TextView 文本变回之前的内容了
- activity_main.xml 布局文件内容如下:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="1"
android:orientation="horizontal"
android:rowCount="2">
<TextView
android:id="@+id/myTextView"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFCCCC"
android:text="0"
android:textSize="50sp" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="回退" />
</GridLayout>
- MainActivity 文件内容如下:
package com.example.administrator.helloworld;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
/**
* 一个应用程序可以有1个或多个活动,而没有任何限制。
* 每个为应用程序所定义的活动都需要在 AndroidManifest.xml 中声明,应用的主活动的意图过滤器标签中需要包含 MAIN 动作和 LAUNCHER 类别
* 如果 MAIN 动作还是 LAUNCHER 类别没有在活动中声明,那么应用程序的图标将不会出现在主屏幕的应用列表中。
*/
public class MainActivity extends AppCompatActivity {
String msg = "Android : ";
/**
* 当活动第一次被创建时调用
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 从项目的 res/layout 中的XML文件加载 UI 组件
* android.util.Log#d(java.lang.String, java.lang.String) 方法用于在 Logcat 窗口中打印日志
*/
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
/** 为 按钮绑定单击事件
* 就和 js 中为某个 id 的标签绑定事件差不多*/
Button button = findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
/**重写点击事件的处理方法onClick()
* 动态修改 TextView 的文本值
* 显示 Toast 信息,这个消息会显示到用户界面上,短暂显示后会消失
* */
@Override
public void onClick(View v) {
TextView textView = findViewById(R.id.myTextView);
textView.setText("Game Over");
Toast.makeText(getApplicationContext(), "你点击了按钮:" + new Date().toString(), Toast.LENGTH_SHORT).show();
}
});
}
/**
* 当活动即将可见时调用
*/
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}
/**
* 当活动可见时调用
*/
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
}
/**
* 当其他活动获得焦点时调用
*/
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() event");
}
/**
* 当活动不再可见时调用
*/
@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() event");
}
/**
* 当活动将被销毁时调用
*/
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}
- 操作步骤如下,显然屏幕由竖屏切换为横屏时,Activity 的数据返回了原始状态了,同时 Logcat 日志信息打印如下:
09-20 06:35:04.290 4776-4776/com.example.administrator.helloworld D/Android :: The onPause() event
The onStop() event
The onDestroy() event
09-20 06:35:04.300 4776-4776/com.example.administrator.helloworld D/Android :: The onCreate() event
The onStart() event
09-20 06:35:04.310 4776-4776/com.example.administrator.helloworld D/Android :: The onResume() event
禁止屏幕横竖屏自动切换
- 在 AndroidManifest.xml 中为<Activity 添加一个属性:android:screenOrientation,可以禁止屏幕横竖屏自动切换,有下述可选值:
- unspecified: 默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向。
- landscape: 横屏显示(宽比高要长)
- portrait: 竖屏显示(高比宽要长)
- user: 用户当前首选的方向
- behind: 和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
- sensor: 由物理的感应器来决定,如用户旋转设备,屏幕会横竖屏切换
- nosensor:忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。
- 以上面的代码为例,现在修改 AndroidManifest.xml 文件,为 Activity 添加 android:screenOrientation 属性,值为横屏显示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MainService" />
</application>
</manifest>
- 效果如下所示,无论手机如何转到,屏幕方向都是固定的: