Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android从应用程序开始就保持方向性_Android_Screen Orientation - Fatal编程技术网

Android从应用程序开始就保持方向性

Android从应用程序开始就保持方向性,android,screen-orientation,Android,Screen Orientation,是否可以根据应用程序启动时使用的方向设置应用程序的方向 示例 我的意思是,假设用户在横向环境中启动了我的应用程序。O希望它保存该状态,不让用户通过转动手机来改变方向(我想锁定它,直到用户重新运行应用程序,然后应用程序再次检查用户是在横向还是纵向启动) 代码(到目前为止) 这是我在这里找到的东西。我可以这样做吗?将其放在我的第一个活动中,然后对其余活动使用此处的方向状态。您可以使用ActivityInfo类来实现它 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

是否可以根据应用程序启动时使用的方向设置应用程序的方向

示例

我的意思是,假设用户在横向环境中启动了我的应用程序。O希望它保存该状态,不让用户通过转动手机来改变方向(我想锁定它,直到用户重新运行应用程序,然后应用程序再次检查用户是在横向还是纵向启动)

代码(到目前为止)


这是我在这里找到的东西。我可以这样做吗?将其放在我的第一个活动中,然后对其余活动使用此处的方向状态。

您可以使用ActivityInfo类来实现它

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
检查活动信息


如果任何人想要获得有意义的方向描述(如通过反转LandSCAPE、传感器景观等传递给OnConfiguration Changed(..),只需使用getRequestedOrientation()

您可以使用ActivityInfo类来实现它

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
检查活动信息


如果任何人想要获得有意义的方向描述(如通过反向LandSCAPE、Sensor横向等传递给OnConfiguration Changed(..),只需使用getRequestedOrientation()

您可以使用SharedReferences来存储即使应用程序关闭也会保存的值。
只需创建一个类SaveOrientation:

public class SaveOrientation {

    static final String ORIENTATION = "orientation";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    // value will be between 0 and 1
    public static void setOrientation(Context ctx, int value) {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putInt(ORIENTATION, value);
        editor.commit();
    }

    public static String getOrientation(Context ctx) {
        return getSharedPreferences(ctx).getString(ORIENTATION, "");
    }

}
然后在你的主要活动中:

int portrait = 0;
int landscape = 1;
.
.
.
// in your onCreate :
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         SaveOrientation.setOrientation(this,portrait);
 }else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
         SaveOrientation.setOrientation(this,landscape);
}
applyOrientation();
.
.
.
// method applyOrientation
private void applyOrientation(){
   int orientation = SaveOrientation.getOrientation(this);

   if(orientation == portrait){
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

   }else if (orientation == landscape){
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

您可以使用SharedReferences存储一个值,即使应用程序已关闭,该值也将被保存。
只需创建一个类SaveOrientation:

public class SaveOrientation {

    static final String ORIENTATION = "orientation";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    // value will be between 0 and 1
    public static void setOrientation(Context ctx, int value) {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putInt(ORIENTATION, value);
        editor.commit();
    }

    public static String getOrientation(Context ctx) {
        return getSharedPreferences(ctx).getString(ORIENTATION, "");
    }

}
然后在你的主要活动中:

int portrait = 0;
int landscape = 1;
.
.
.
// in your onCreate :
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         SaveOrientation.setOrientation(this,portrait);
 }else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
         SaveOrientation.setOrientation(this,landscape);
}
applyOrientation();
.
.
.
// method applyOrientation
private void applyOrientation(){
   int orientation = SaveOrientation.getOrientation(this);

   if(orientation == portrait){
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

   }else if (orientation == landscape){
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

感谢您提到ActivityInfo,我不知道这一点,但我采用了Davids的方法。对于解释+示例非常有用,谢谢你提到ActivityInfo,我不知道,但我采用了Davids的方法。对于解释+例子来说真的很有帮助