【问题标题】:how to set landscape orientation in particular activity?如何在特定活动中设置横向?
【发布时间】:2017-10-11 11:08:32
【问题描述】:

我正在使用图表视图在我的活动中显示图表。现在,我需要根据用户的旋转来设置活动的方向。意味着,如果用户将水平握住手机,则活动应将其方向设置为“横向”,垂直方向相同,即“纵向”。它不应该取决于移动设置轮换。我只希望这个用于我的图表活动。

这是您可以轻松理解的简短屏幕。

enter image description here

【问题讨论】:

  • @SunishaSindhu 不,它不工作
  • 如果您在清单中声明方向,它将在编译时永久设置。如果将其设置为纵向,则活动将始终为纵向。如果您查看我的答案,您会发现它在运行时有效。
  • 不,我没有在清单中添加任何方向。
  • 您是否在应用程序标签中添加了方向(在您的清单中)
  • @Tabishkhan 你试过我的答案了吗?

标签: android xml material-design


【解决方案1】:

要在运行时锁定您的 Activity,您可以使用

// lock orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

您现在只需要确定当前方向,并传递它以保持锁定状态。

首先,添加这些导入:

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;

然后您可以将其添加到您的活动中onCreate()

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // lock the current device orientation    
    int currentOrientation = this.getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_PORTRAIT){
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION‌​_PORTRAIT);
    }
    else{
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION‌​_LANDSCAPE);
    }

    // ... rest of your code 

}

有关其他方向配置,请参阅ActivityInfo documentation

【讨论】:

  • 此消息在添加您的代码后显示,无法解析符号 'SCREEN_ORIENTATION_PORTRAIT'
  • 查看代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); int currentOrientation = this.getResources().getConfiguration().orientation; if (currentOrientation == Configuration.ORIENTATION_PORTRAIT){ this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else{ this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
  • 啊,我明白了,你加的太多了。第一次提到 setRequestedOrientation 只是解释性的。您只需要将我发布的最后一个代码块添加到您的 onCreate() 中。我会更新答案。
  • 非常感谢。它现在工作。但是为什么在实现此代码后,我的应用程序运行缓慢,就像挂起一样。有什么解决方案或原因吗?
【解决方案2】:

打开 AndroidManifest.xml 并将以下内容添加到活动的属性android:screenOrientation="landscape" 例如

<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name=".MainActivity">

【讨论】:

  • 它的开放式直接景观我不希望它应该取决于用户。
  • 你问过how to set landscape orientation in particular activity?,但你也想问set the orientation of activity according to user's rotation。选择其中之一
  • 他没有将其指定为横向。他询问如何在运行时将其设置为横向或纵向。
  • 我在我的问题中提到了它。如果用户将水平握住手机,则活动应将其方向设置为“横向”,垂直方向相同,即“纵向”。它不应该依赖于移动设置旋转,无论是旋转还是关闭都没有关系。
  • 嗯,这个描述比OP发布的描述更清楚。两个用户未能理解这个问题,因为标题只是说 how to set landscape orientation in particular activity? 并且 OP 没有提到“基于 输入的方向锁定屏幕方向”。输入的是这里的关键字
【解决方案3】:

在您的清单文件中添加以下代码,您需要将哪个 Activity 显示为横向,

        <activity android:name=".activity name of yours"
        android:configChanges="orientation|screenSize|keyboardHidden">
        </activity>

    <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:configChanges="orientation|screenSize|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


</application>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2022-01-21
    相关资源
    最近更新 更多