如何更改屏幕方向,而不在Android上创建新活动?

Cru*_*ces 4 android screen-orientation

我不知道标题是否正确,这是怎么回事.

我有一个应用程序在手机和平​​板电脑上的工作方式不同,在手机上它显示为平板电脑上的肖像,它显示为风景.

为了实现这一点,我创建了一个名为CoreActivity的类,它由我的所有活动扩展,并执行以下操作:

public class CoreActivity extends Activity {
    protected boolean _landscape = false;

    public boolean isPhone() {
        int layoutSize = getScreenLayoutSize();
        return (layoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || layoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isPhone() && !_landscape) {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }


    protected int getScreenLayoutSize() {
        return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望在横向模式下在手机设置上显示屏幕时出现问题,为此我使用以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    _landscape = true
    super.onCreate(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)

问题是,在手机上,如果用户手持手机处于纵向模式(因为大多数应用程序处于纵向模式),则会创建并销毁活动,然后重新创建.但如果他们将它保持在横向模式,那么它只创建一次.

我的问题出现是因为在onCreate方法中我启动了加载数据的线程,我也显示了片段.

有没有办法避免这个问题?有没有办法从纵向模式开始启动活动而不更改它,或者不创建两次?

提前感谢您提供的任何帮助

gun*_*ess 12

重新发生只是因为你强迫,setRequestedOrientation可能是为了设置屏幕方向.但是,您无需通过代码检查和更改它.你可以通过xml文件来完成.您可以根据屏幕大小设置不同的xml文件.但它有点黑客,因此未来无法保证.

在清单文件上,您可以强制执行:

<activity
   android:name="com.my.example.MyActivity"
   android:screenOrientation="landscape"/>  // or portrait
Run Code Online (Sandbox Code Playgroud)

因此,据我所知,您希望强制使用小尺寸和横向(或传感器)的肖像以获得更大的屏幕尺寸.但以上配置适用于所有屏幕尺寸.这是棘手的部分:landscape portrait sensor等等都是这里定义的整数.正如你可能猜到的,你可以写android:screenOrientation=0而不是landscape.我们从android的开始课程中知道,我们可以在xml文件中定义整数,然后它们的值可能会因屏幕大小而异.所以..

您应该首先integers.xml为不同的屏幕尺寸创建不同的文件.即:

values
 - integers.xml
values-large
 - integers.xml
Run Code Online (Sandbox Code Playgroud)

用于values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">1</integer>  // 1 for portrait
</resources>
Run Code Online (Sandbox Code Playgroud)

用于values-large/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">0</integer> // 0 for landscape
</resources>
Run Code Online (Sandbox Code Playgroud)

最后你必须通过以下方式操作清单文件:

<activity
   android:name="com.my.example.MyActivity"
   android:screenOrientation="@integer/orientation"/>  // read constant from xml files
Run Code Online (Sandbox Code Playgroud)

你也可以使用sensor,fullSensor,nosensor,locked,behind,reverseLandscape,reversePortait选择太多.但我警告你,这是一个黑客解决方案.

  • 现在,这在Android Studio中给出了一个红色下划线警告,指出您不能使用Manifest中的资源值以这种方式在设备配置之间进行更改.这很有意义,因为Manifest是APK中的静态文件,所以这些引用必须在构建时解析. (13认同)
  • 不适合我.`@ integer/orientation`总是取自`values /`目录,即使在我知道它应该从更合格的目录如`values-sw600dp /`获取它的设备上.奇怪的是,如果你在运行时通过`getResources()得到整数.getInteger(..)`它可以从更合格的目录中选择.对我而言,安装程序中的错误似乎是解析`AndroidManifest.xml`.在查找整数资源时,它不使用诸如`-swNNNdp`之类的限定符来搜索最佳匹配资源. (2认同)

Cro*_*ono 5

好吧,我找到了解决此问题的方法,只需声明:

android:screenOrientation="locked"
Run Code Online (Sandbox Code Playgroud)

在清单中存在此问题的每个活动上。

并继续以setRequestedOrientation()编程方式使用来定义onCreate()方法中的横向还是纵向,

它会起作用!;)