欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android横竖屏幕切换生命周期详解

程序员文章站 2023-11-27 15:01:10
一、简介 二、代码 /activitylifecycle_3screen/androidmanifest.xml

一、简介

Android横竖屏幕切换生命周期详解

二、代码

Android横竖屏幕切换生命周期详解

/activitylifecycle_3screen/androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.fry.activitylifecycle_3screen"
  android:versioncode="1"
  android:versionname="1.0" >

  <uses-sdk
    android:minsdkversion="8"
    android:targetsdkversion="19" />

  <application
    android:allowbackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/apptheme" >
    <activity
      android:configchanges="keyboardhidden|orientation|screensize"
      android:name="com.fry.activitylifecycle_3screen.mainactivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.main" />

        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <activity android:name="com.fry.activitylifecycle_3screen.activity01" ></activity>
    <activity android:name="com.fry.activitylifecycle_3screen.activity02" android:theme="@android:style/theme.translucent" ></activity>
  </application>

</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.fry.activitylifecycle_3screen"
  android:versioncode="1"
  android:versionname="1.0" >

  <uses-sdk
    android:minsdkversion="8"
    android:targetsdkversion="19" />

  <application
    android:allowbackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/apptheme" >
    <activity
      android:configchanges="keyboardhidden|orientation|screensize"
      android:name="com.fry.activitylifecycle_3screen.mainactivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.main" />

        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <activity android:name="com.fry.activitylifecycle_3screen.activity01" ></activity>
    <activity android:name="com.fry.activitylifecycle_3screen.activity02" android:theme="@android:style/theme.translucent" ></activity>
  </application>

</manifest>

核心代码:android:configchanges="keyboardhidden|orientation|screensize"

com.fry.activitylifecycle_3screen.mainactivity

package com.fry.activitylifecycle_3screen;

import com.fry.activitylifecycle_3screen.r;

import android.app.activity;
import android.content.intent;
import android.content.res.configuration;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;



public class mainactivity extends activity implements onclicklistener{
  private button btn_pause;//创建一个button对象
  private button btn_stop;
  private button btn_offline;
  private string tag=mainactivity.class.getsimplename(); 
   protected void oncreate(bundle savedinstancestate) {
      super.oncreate(savedinstancestate);//父类操作
      setcontentview(r.layout.activity_main);//引入名为activity_main的界面
      btn_pause=(button) findviewbyid(r.id.btn_pause);//找id为btn_pause的button
      btn_stop=(button) findviewbyid(r.id.btn_stop);//找id为btn_stop的button
      btn_offline=(button) findviewbyid(r.id.btn_offline);
      btn_pause.setonclicklistener(this);
      btn_stop.setonclicklistener(this);
      btn_offline.setonclicklistener(this);
      /*
       * activity被创建时执行
       */
      log.d(tag, "oncreate");
      
   
    }
   /*
   * activity可见时执行
   */
   @override
  protected void onstart() {
    // todo auto-generated method stub
    super.onstart();
    log.d(tag, "onstart");
  }
   
   /*
   * activity交互时执行
   */
  @override
  protected void onresume() {
    // todo auto-generated method stub
    super.onresume();
    log.d(tag, "onresume");
  }
  /*
   * activity重新可见时执行
   */
  @override
  protected void onrestart() {
    // todo auto-generated method stub
    super.onrestart();
    log.d(tag, "onrestart");  
  }
  /*
   * activity暂停时执行
   */
  @override
  protected void onpause() {
    // todo auto-generated method stub
    super.onpause();
    log.d(tag, "onpause");
  }
  /*
   * activity停止时执行
   */
  @override
  protected void onstop() {
    // todo auto-generated method stub
    super.onstop();
    log.d(tag, "onstop");
  }
  /*
   * activity销毁时执行
   */
  @override
  protected void ondestroy() {
    // todo auto-generated method stub
    super.ondestroy();
    log.d(tag, "ondestroy");
  }
  /*
   * activity在配置改变时执行
   * 比如横竖屏幕的切换,键盘有无的切换,屏幕大小的改变
   */
  @override
  public void onconfigurationchanged(configuration newconfig) {
    // todo auto-generated method stub
    super.onconfigurationchanged(newconfig);
    log.d(tag, "onconfigurationchanged");
  }
  @override
  public void onclick(view v) {
    // todo auto-generated method stub
    switch (v.getid()) {
    case r.id.btn_pause:
      intent intent=new intent();
      intent.setclass(this, activity02.class);
      startactivity(intent);
      break;
    case r.id.btn_stop:
      intent intent2=new intent();
      intent2.setclass(this, activity01.class);
      startactivity(intent2);
      break;
    case r.id.btn_offline://断开状态
      finish();
    default:
      break;
    }
  }
}

三、一直横屏或者一直竖屏

很多手机游戏里面一进去就是横屏,而且不能切换为竖屏,那么怎么样达到这样的效果呢?

 /activitylifecycle_3screen/androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.fry.activitylifecycle_3screen"
 android:versioncode="1"
 android:versionname="1.0" >

 <uses-sdk
  android:minsdkversion="8"
  android:targetsdkversion="19" />

 <application
  android:allowbackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/apptheme" >
  <activity
   android:screenorientation="portrait"
   android:name="com.fry.activitylifecycle_3screen.mainactivity"
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="android.intent.action.main" />

    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
  <activity android:name="com.fry.activitylifecycle_3screen.activity01" ></activity>
  <activity android:name="com.fry.activitylifecycle_3screen.activity02" android:theme="@android:style/theme.translucent" ></activity>
 </application>

</manifest>

android:screenorientation="landscape"横屏
android:screenorientation="portrait"竖屏

四、如何获取手机是横屏还是竖屏

Android横竖屏幕切换生命周期详解

com.fry.activitylifecycle_3screen.mainactivity

package com.fry.activitylifecycle_3screen;
import com.fry.activitylifecycle_3screen.r;

import android.app.activity;
import android.content.intent;
import android.content.res.configuration;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;



public class mainactivity extends activity implements onclicklistener{
 private button btn_pause;//创建一个button对象
 private button btn_stop;
 private button btn_offline;
 private string tag=mainactivity.class.getsimplename(); 
  protected void oncreate(bundle savedinstancestate) {
   super.oncreate(savedinstancestate);//父类操作
   setcontentview(r.layout.activity_main);//引入名为activity_main的界面
   btn_pause=(button) findviewbyid(r.id.btn_pause);//找id为btn_pause的button
   btn_stop=(button) findviewbyid(r.id.btn_stop);//找id为btn_stop的button
   btn_offline=(button) findviewbyid(r.id.btn_offline);
   btn_pause.setonclicklistener(this);
   btn_stop.setonclicklistener(this);
   btn_offline.setonclicklistener(this);
   /*
    * activity被创建时执行
    */
   log.d(tag, "oncreate");
   
  
  }
  /*
  * activity可见时执行
  */
  @override
 protected void onstart() {
  // todo auto-generated method stub
  super.onstart();
  log.d(tag, "onstart");
 }
  
  /*
  * activity交互时执行
  */
 @override
 protected void onresume() {
  // todo auto-generated method stub
  super.onresume();
  log.d(tag, "onresume");
 }
 /*
  * activity重新可见时执行
  */
 @override
 protected void onrestart() {
  // todo auto-generated method stub
  super.onrestart();
  log.d(tag, "onrestart"); 
 }
 /*
  * activity暂停时执行
  */
 @override
 protected void onpause() {
  // todo auto-generated method stub
  super.onpause();
  log.d(tag, "onpause");
 }
 /*
  * activity停止时执行
  */
 @override
 protected void onstop() {
  // todo auto-generated method stub
  super.onstop();
  log.d(tag, "onstop");
 }
 /*
  * activity销毁时执行
  */
 @override
 protected void ondestroy() {
  // todo auto-generated method stub
  super.ondestroy();
  log.d(tag, "ondestroy");
 }
 /*
  * activity在配置改变时执行
  * 比如横竖屏幕的切换,键盘有无的切换,屏幕大小的改变
  */
 @override
 public void onconfigurationchanged(configuration newconfig) {
  // todo auto-generated method stub
  super.onconfigurationchanged(newconfig);
  log.d(tag, "onconfigurationchanged");
  int width=getwindowmanager().getdefaultdisplay().getwidth();
  int height=getwindowmanager().getdefaultdisplay().getheight();
  if(width>height) log.d(tag, "landscape");
  else log.d(tag, "portrait");
 }
 @override
 public void onclick(view v) {
  // todo auto-generated method stub
  switch (v.getid()) {
  case r.id.btn_pause:
   intent intent=new intent();
   intent.setclass(this, activity02.class);
   startactivity(intent);
   break;
  case r.id.btn_stop:
   intent intent2=new intent();
   intent2.setclass(this, activity01.class);
   startactivity(intent2);
   break;
  case r.id.btn_offline://断开状态
   finish();
  default:
   break;
  }
 }
}

/activitylifecycle_3screen/androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.fry.activitylifecycle_3screen"
 android:versioncode="1"
 android:versionname="1.0" >

 <uses-sdk
  android:minsdkversion="8"
  android:targetsdkversion="19" />

 <application
  android:allowbackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/apptheme" >
  <activity
   android:configchanges="keyboardhidden|orientation|screensize"
   android:name="com.fry.activitylifecycle_3screen.mainactivity"
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="android.intent.action.main" />

    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
  <activity android:name="com.fry.activitylifecycle_3screen.activity01" ></activity>
  <activity android:name="com.fry.activitylifecycle_3screen.activity02" android:theme="@android:style/theme.translucent" ></activity>
 </application>

</manifest>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。