Android强制设定横屏时,SurfaceView一直黑屏

  接着上一个问题,解决了SurfaceView闪屏问题之后(http://www.cnblogs.com/Joanna-Yan/p/4829325.html),又有了一个新的问题。现在我想设置含有fragment+viewpager的activity横屏。其中一个fragment有视频播放功能,含SurfaceView.

当我横屏拿着平板时,打开程序进入到该activity,是正常的。当竖屏拿着打开程序进入到该activity时,就会一直处于黑屏的状态。原因应该还是SurfaceView。难道程序转入后台或者黑屏以后(只要是不绘制状态),屏幕方向就是系统默认的屏幕方向吗?

解决:

1.在AndroidManifest.xml的对应的activty中,设置android:screenOrientation="nosensor",即 忽略物理感应器,这样就不会随着用户旋转设备而更改了 ( "unspecified"设置除外 )

设置android:configChanges="orientation|keyboardHidden|keyboard",横竖屏切换时,不会重新加载页面。

2.在对应的Activity中设置横屏。setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

相关详解:关于Android设置全屏和横屏

横屏设置

XML文件设置--portrait为纵向,landscape为横向

android:screenOrientation=["unspecified" | "user" | "behind" |"landscape" | "portrait" | "sensor" | "nonsensor"]

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

全屏两种方法:

方法一:java代码

public void onCreate(Bundle savedInstanceState) {            
    super.onCreate(savedInstanceState);                         
    //设置无标题            
    requestWindowFeature(Window.FEATURE_NO_TITLE);             
    //设置全屏            
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
WindowManager.LayoutParams.FLAG_FULLSCREEN);                         
    setContentView(R.layout.main);     
}  

方法二:xml布局

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.andyidea"  
    android:versionCode="1"  
    android:versionName="1.0" >  
  
    <uses-sdk android:minSdkVersion="8" />  
  
    <application  
        android:icon="@drawable/icon"  
        android:label="@string/app_name" >  
        <activity  
            android:name=".login.LoginActivity"  
            android:label="@string/app_name"  
            android:theme="@android:style/android.NoTitleBar.Fullscreen" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
  
</manifest>  

 

原文地址:https://www.cnblogs.com/Joanna-Yan/p/4831371.html