Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.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_Orientation - Fatal编程技术网

Android:如何设置视图的方向

Android:如何设置视图的方向,android,orientation,Android,Orientation,我有一个服务,在启动时,一个视图显示在所有视图的顶部 如果我将手机旋转到横向,则视图方向将更改为横向,但我只希望视图为纵向。如何执行此操作?当视图打开时,尝试通过编程设置方向 setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture) 当视图打开时,尝试通过编程设置方向 setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture) 在清单文件中

我有一个服务,在启动时,一个视图显示在所有视图的顶部


如果我将手机旋转到横向,则视图方向将更改为横向,但我只希望视图为纵向。如何执行此操作?

当视图打开时,尝试通过编程设置方向


setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture)

当视图打开时,尝试通过编程设置方向


setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture)

在清单文件中,确保设置正确的方向,如下所示:

<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    View yourView = ....;

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        yourView.setRotation(-90);
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        yourView.setRotation(90);
    }
}


这样,纵向模式是强制的,当您旋转设备时,您的应用程序将不会更改为横向模式。

在清单文件中,确保设置正确的方向,如下所示:

<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
@Override
public void onCreate(Bundle savedInstanceState) {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }
@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    View yourView = ....;

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        yourView.setRotation(-90);
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        yourView.setRotation(90);
    }
}

这样,人像模式是强制的,当您旋转设备时,您的应用程序将不会更改为横向模式

@Override
public void onCreate(Bundle savedInstanceState) {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }


要声明您的活动处理配置更改,请编辑相应的

编辑:

Android屏幕定位生命周期

我们可能会对android活动的android屏幕方向更改生命周期感到困惑。有时,当您旋转设备时,您可能会发现您的活动正在重新启动。有时候什么事也没发生。以下是android屏幕方向更改生命周期的简单规则

1。如果您已经
@覆盖
java代码(在Android活动类中)中用于处理Android屏幕方向更改的
onConfiguration Changed()
函数。然后,您的活动将永远不会因任何屏幕方向更改而重新启动

2。如果您没有像上面那样
@覆盖onConfigurationChanged()
功能,那么每当设备中发生任何屏幕方向更改时,您的运行活动都将重新启动。这意味着您的活动将首先通过调用
onDestroy()
API进行销毁,然后
onCreate()
方法将再次调用您正在运行的活动以重新启动它

3。要正确处理活动的重新启动状态,您需要在正常的活动生命周期中恢复其以前的状态,在正常的活动生命周期中,Android操作系统在销毁活动之前调用
onSaveInstanceState()
方法。这样您就可以保存数据(变量值)和应用程序状态。然后,您可以在活动的
onCreate()
onRestoreInstanceState()
方法调用期间还原以前的状态


希望它能帮助您。

要声明您的活动处理配置更改,请编辑相应的

编辑:

Android屏幕定位生命周期

我们可能会对android活动的android屏幕方向更改生命周期感到困惑。有时,当您旋转设备时,您可能会发现您的活动正在重新启动。有时候什么事也没发生。以下是android屏幕方向更改生命周期的简单规则

1。如果您已经
@覆盖
java代码(在Android活动类中)中用于处理Android屏幕方向更改的
onConfiguration Changed()
函数。然后,您的活动将永远不会因任何屏幕方向更改而重新启动

2。如果您没有像上面那样
@覆盖onConfigurationChanged()
功能,那么每当设备中发生任何屏幕方向更改时,您的运行活动都将重新启动。这意味着您的活动将首先通过调用
onDestroy()
API进行销毁,然后
onCreate()
方法将再次调用您正在运行的活动以重新启动它

3。要正确处理活动的重新启动状态,您需要在正常的活动生命周期中恢复其以前的状态,在正常的活动生命周期中,Android操作系统在销毁活动之前调用
onSaveInstanceState()
方法。这样您就可以保存数据(变量值)和应用程序状态。然后,您可以在活动的
onCreate()
onRestoreInstanceState()
方法调用期间还原以前的状态


希望能对您有所帮助。

请在下面找到一个例子,为您完成任务提供更好的思路

public class MainScreenOrientation extends Activity {

    private static final String TAG = "MainScreenOrientation";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.main_activity);

        startService(new Intent(this, MyOwnService.class) );
    }
}
在我的服务课上

public class MyOwnService extends Service {
    private static final String TAG = "MyOwnService";
    private static Context mContext;
    private static final String BCAST_CONFIGCHANGED = "android.intent.action.CONFIGURATION_CHANGED";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        mContext = this;

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BCAST_CONFIGCHANGED);
        this.registerReceiver(mBroadcastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {           
        Log.d(TAG, "onDestroy()");
        //Unregister receiver to avoid memory leaks
        mContext.unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public void onStart(Intent intent, int startid) {

    }

    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent myIntent) {

            if (myIntent.getAction().equals( BCAST_CONFIGCHANGED)) {

                Log.d(TAG, "received->" + BCAST_CONFIGCHANGED);


                if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                    // it's Landscape
                    Log.d(TAG, "LANDSCAPE");
                }
                else {
                    Log.d(TAG, "PORTRAIT");
                }
            }
        }
    };
}
最后在清单文件中

<service android:enabled="true"  android:name="com.android.example.services.MyOwnService">
            <intent-filter>
                <action android:name="android.intent.action.CONFIGURATION_CHANGED"/>
            </intent-filter>
</service>


希望能对您有所帮助。

请在下面找到一个例子,为您完成任务提供更好的思路

public class MainScreenOrientation extends Activity {

    private static final String TAG = "MainScreenOrientation";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.main_activity);

        startService(new Intent(this, MyOwnService.class) );
    }
}
在我的服务课上

public class MyOwnService extends Service {
    private static final String TAG = "MyOwnService";
    private static Context mContext;
    private static final String BCAST_CONFIGCHANGED = "android.intent.action.CONFIGURATION_CHANGED";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        mContext = this;

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BCAST_CONFIGCHANGED);
        this.registerReceiver(mBroadcastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {           
        Log.d(TAG, "onDestroy()");
        //Unregister receiver to avoid memory leaks
        mContext.unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public void onStart(Intent intent, int startid) {

    }

    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent myIntent) {

            if (myIntent.getAction().equals( BCAST_CONFIGCHANGED)) {

                Log.d(TAG, "received->" + BCAST_CONFIGCHANGED);


                if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                    // it's Landscape
                    Log.d(TAG, "LANDSCAPE");
                }
                else {
                    Log.d(TAG, "PORTRAIT");
                }
            }
        }
    };
}
最后在清单文件中

<service android:enabled="true"  android:name="com.android.example.services.MyOwnService">
            <intent-filter>
                <action android:name="android.intent.action.CONFIGURATION_CHANGED"/>
            </intent-filter>
</service>


希望对您有所帮助。

如果我理解正确,您希望在纵向模式下固定视图,但在旋转设备时,应用程序的其余部分可以自由旋转

@Override
public void onCreate(Bundle savedInstanceState) {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }
在这种情况下,需要捕捉方向更改,然后逆当前旋转方向旋转视图

大概是这样的:

<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    View yourView = ....;

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        yourView.setRotation(-90);
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        yourView.setRotation(90);
    }
}
您还必须编辑清单文件中的相应元素

      <activity android:name=".MyActivity"
         android:configChanges="orientation|keyboardHidden"
         android:label="@string/app_name">

我不确定度值,现在无法检查,您可能需要测试


不过,这样你就可以让你的视图始终处于纵向模式。

如果我理解正确,你希望在纵向模式下固定视图,但只要你旋转设备,应用程序的其余部分就可以自由旋转

@Override
public void onCreate(Bundle savedInstanceState) {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }
在这种情况下,需要捕捉方向更改,然后逆当前旋转方向旋转视图

大概是这样的:

<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    View yourView = ....;

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        yourView.setRotation(-90);
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        yourView.setRotation(90);
    }
}
您还必须编辑清单文件中的相应元素

      <activity android:name=".MyActivity"
         android:configChanges="orientation|keyboardHidden"
         android:label="@string/app_name">

我不确定度值,现在无法检查,您可能需要测试

然而,这样你