【问题标题】:Android cannot set screenOrientation,Android 无法设置 screenOrientation,
【发布时间】:2016-08-09 23:35:58
【问题描述】:

我已经阅读了在此站点和其他站点上可以找到的所有帖子并遵循了建议,但在最简单的实现中,当 Note 5 处于横向时,我无法将其设置为纵向模式。 我将建议的行添加到清单文件中,重新构建,应用程序以横向模式启动。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.twistlogic.miwidget">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我也试过用代码来做这件事,这是我的主要目的:

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

没有产生任何有效的东西。

我正在尝试学习 Android,所以我知道这很可能是由于我的知识有限。我要做的是修复方向并将其锁定在那里,然后仅在用户按下按钮时才允许它更改。我确实读过必须在 API 13 之后设置屏幕尺寸。但我不确定如何设置。创建项目时,我正在使用来自基本活动的片段模板屏幕。

感谢您的帮助。

【问题讨论】:

    标签: android-activity orientation screen-orientation


    【解决方案1】:

    这个可以在Main Activity里设置我没试过在Manifest里设置这里就一行代码几行写测试好了这里是Manifest代码在最后

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
    int orientation = getResources().getConfiguration().orientation;
    if(orientation ==  Configuration.ORIENTATION_PORTRAIT){
    
    }else if(orientation ==  Configuration.ORIENTATION_LANDSCAPE){}
    

    在您的主要活动之后的清单文件中粘贴以下行。

    android:screenOrientation="portrait"

    我没有在 Manifest 文件中测试过这段代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多