天嵌 ARM开发社区

 找回密码
 注册
查看: 2797|回复: 0

Android强制设置横屏或竖屏(转)

[复制链接]
freewing 发表于 2016-4-13 10:35:31 | 显示全部楼层 |阅读模式
  全屏
  在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
  2. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  3.   WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
复制代码


  横屏

  按照下面代码示例修改Activity的onResume方法
  1. @Override
  2. protected void onResume() {
  3. /**
  4.   * 设置为横屏
  5.   */
  6. if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
  7.   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  8. }
  9. super.onResume();
  10. }
复制代码

  或者在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)
  1. android:launchMode="singleTask" android:screenOrientation="portrait">
复制代码

  要设置成竖屏设置成 SCREEN_ORIENTATION_PORTRAIT
  // ----------------
  常亮
  view.setKeepScreenOn(true)
  不加任何旋转屏幕的处理代码的时候,旋转屏幕将会导致系统把当前activity关闭,重新打开。
  如果只是简单的界面调整,我们可以阻止此问题的发生,屏幕旋转而自己调整屏幕的元素重构。
  首先我们需要修改AndroidManifest.xml文件:
  1. <activity android:name=".Magazine">
  2. </activity>
  3. //修改为:
  4. <activity android:name=".Magazine"
  5.   android:configChanges="orientation|keyboard">
  6. </activity>
  7. 这样是让程序能够响应旋转屏幕的事件。
  8. 然后重写onConfigurationChanged方法:
  9. @Override
  10. public void onConfigurationChanged(Configuration newConfig) {
  11.   // TODO Auto-generated method stub
  12.   super.onConfigurationChanged(newConfig);
  13.   Log.v(" == onConfigurationChanged");
  14.   processLayout();
  15. }

  16. //----------------------------
复制代码

  在我们用Android开发过程中,会碰到Activity在切换到后台或布局从横屏LANDSCAPE切换到PORTRAIT,会重新切换Activity会触发一次onCreate方法。
  在Android开发中这种情况视可以避免的,我们可以在androidmanifest.xml中的activit元素加入这个属性 android:configChanges="orientation|keyboardHidden" 就能有效避免oncreat方法的重复加载,
  androidmanifest.xml内容如下:红色字体为添加部分
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.demo"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".DemoGPS"
  8.             android:configChanges="orientation|keyboardHidden"
  9.                   android:label="@string/app_name">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN" />
  12.                 <category android:name="android.intent.category.LAUNCHER" />
  13.             </intent-filter>
  14.         </activity>
  15.   <uses-library android:name="com.google.android.maps" />
  16.     </application>
  17.     <uses-sdk android:minSdkVersion="7" />
  18. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  19.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
  20.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

  21. </manifest>
复制代码

  同时在Activity的Java文件中重载onConfigurationChanged(Configuration newConfig)这个方法,这样就不会在布局切换或窗口切换时重载等方法。代码如下:
  1. @Override
  2.     public void onConfigurationChanged(Configuration newConfig)
  3.     {
  4.         super.onConfigurationChanged(newConfig);
  5.      if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
  6.      {
  7. //land
  8.      }
  9.      else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
  10.      {
  11. //port
  12.      }
  13.     }

  14. //------------------------------------------------------
复制代码

  关于Android中Activity的横竖屏切换问题可以通过AndroidManifest.xml文件中的Activity来配置:
  1. android:screenOrientation=["unspecified" | "user" | "behind" |
  2.           "landscape" | "portrait" |
  3.           "sensor" | "nonsensor"]
复制代码

  screenOrientation 用来指定Activity的在设备上显示的方向,每个值代表如下含义:

"unspecified" 默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape" 横屏显示(宽比高要长)
"portrait" 竖屏显示(高比宽要长)
"user" 用户当前首选的方向
"behind" 和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
"sensor" 有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
"nosensor" 忽略物理感应器,这样就不会随着用户旋转设备而更改了 ( "unspecified"设置除外 )。









您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号 )

GMT+8, 2024-4-24 02:28 , Processed in 1.046875 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表