gpt4 book ai didi

android - 如何在特定 Activity 中设置横向?

转载 作者:数据小太阳 更新时间:2023-10-29 02:33:36 25 4
gpt4 key购买 nike

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

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

enter image description here

最佳答案

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

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

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

首先,添加这些导入:

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

然后您可以将其添加到您的 Activity 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对于其他方向配置。

关于android - 如何在特定 Activity 中设置横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43932491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com