如何使用Android 5.0在纵向模式下保持屏幕方向?

时间:2016-05-24 13:11:03

标签: java android rotation

我需要将屏幕保持在纵向模式。为此,我在清单文件中使用此行

android:screenOrientation="portrait"

这是我的档案

<activity
        android:name=".activities.Splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

我使用三星S5,它仍然可以横向旋转......

使用Android 5.0获取文档我们获得了广泛的设置选项

android:screenOrientation=["unspecified" | "behind" |
"landscape" | "portrait" |
"reverseLandscape" | "reversePortrait" |
"sensorLandscape" | "sensorPortrait" |
"userLandscape" | "userPortrait" |
"sensor" | "fullSensor" | "nosensor" |
"user" | "fullUser" | "locked"]

我已尝试了其中一些,但它仍在旋转......

我做错了什么?

整个清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.camera2basic"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- For Google+ LogIn -->
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<!-- Для регистрации приложения в GCM всякий раз, когда телефон перезагружается. -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android..RECEIVE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<permission
    android:name="com.example.android.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.android" />

<application
    android:name=".authorization.MyFacebook"
    android:allowBackup="true"
    android:icon="@drawable/title"
    android:label="@string/app_name"
    android:theme="@style/MaterialTheme">
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name=".activities.Splash"
        android:label="@string/app_name"
        android:screenOrientation="locked">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <receiver
        android:name=".gcm.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.example.android" />
        </intent-filter>
    </receiver>

    <service android:name=".gcm.GcmIntentService" />

    <activity
        android:name=".activities.AcceptNotAccept"
        android:theme="@style/AppTheme.CustomStyle" />
    <activity
        android:name=".activities.PopUpActivity"
        android:theme="@style/AppTheme.CustomStyle" />
    <activity android:name=".tools.TestDeleteIt" />
    <activity android:name=".activities.Welcome2" />
    <activity android:name=".activities.WebActivity" />
    <activity android:name=".authorization.AuthorizationActivity" />
    <activity android:name=".activities.WebViewAcceptReject" />
    <activity android:name=".activities.CameraActivity" />
    <activity android:name=".authorization.LogIn" />
    <activity android:name=".authorization.RegistrationActivity" />
    <activity android:name=".activities.ForgotYourPassword" />
    <activity android:name=".activities.MainActivity" />
    <activity android:name=".activities.VideoActivity"/>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>

</manifest>

启动的Java代码

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

//      Hide the status bar.
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);

    TextView tvSplash = (TextView) findViewById(R.id.tvSplash);
    tvSplash.setVisibility(View.INVISIBLE);
    UtilClass.setFont(getApplicationContext(), tvSplash);

    Thread logoTimer = new Thread() {
        public void run() {
            try {

                ImageView imageView = (ImageView) findViewById(R.id.ivSplash);
                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
                imageView.startAnimation(animation);

                int logoTimer = 0;
                while (logoTimer < 2500) {
                    sleep(100);
                    logoTimer = logoTimer + 100;
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView) findViewById(R.id.tvSplash);
                        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
                        textView.startAnimation(animation);

                    }
                });

                logoTimer = 0;

                while (logoTimer < 2500) {
                    sleep(100);
                    logoTimer = logoTimer + 100;
                }

                if (UtilClass.checkLogIn(getApplicationContext())) {
                    startActivity(new Intent(getApplicationContext(), MainActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                    Intent.FLAG_ACTIVITY_CLEAR_TASK));
                } else {
                    startActivity(new Intent(getApplicationContext(), AuthorizationActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                    Intent.FLAG_ACTIVITY_CLEAR_TASK));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    logoTimer.start();
}
}

4 个答案:

答案 0 :(得分:1)

在主要活动后的清单文件中粘贴到下面的行。

android:screenOrientation="portrait"

或者在setContentView()之后的编码中粘贴到下面的行。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

答案 1 :(得分:0)

您可以添加android:configChanges="orientation|keyboardHidden"吗?

<activity
    android:name=".activities.Splash"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden">

答案 2 :(得分:0)

  

在主要活动后的清单文件中粘贴到下面的行。

android:screenOrientation="portrait"
  

java through setContentView()粘贴到行下面。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

see documentation.

答案 3 :(得分:-1)

将方向锁定为当前旋转,无论是什么。在API级别18中添加。

android:screenOrientation="locked"

android:screenOrientation="locked"