5

我的演示应用程序遇到问题。
请检查我附加的屏幕,

纵向视图横向视图

我希望Activity A仅在纵向视图中(不应该在横向模式下更改),所以我ScreenOrientation="protrait"在清单文件中声明并且Activity B(主题是对话框)应该在任何模式下(纵向或横向)更改,但是当我在横向模式下移动手机时,Activity B不会改变它的方向。


活动一

package com.example.activityorientation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent it = new Intent(getApplicationContext(),DialogAct.class);
        startActivity(it);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


活动 B

package com.example.activityorientation;

import android.app.Activity;
import android.os.Bundle;

public class DialogAct extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dailogact);
    }
}


清单文件,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activityorientation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.activityorientation.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name="com.example.activityorientation.DialogAct"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.Dialog"/>

    </application>

</manifest>


需要帮助,谢谢。

4

7 回答 7

0

When an Not Full screen activity is launched on top with unspecified orientation(default) , it follows the orientation of Full screen activity which behind it in pause mode. But if we deliberately specify the orientation value for not full screen activity, it will also force the orientation of Full screen activity which behind it in pause mode to change along with it.

Activity B is not a full screen activity. So, When we launch Activity B , Activity A goes in Pause mode only. Now for such case scenario, if for Activity B screen orientation is unspecified or not defined, it will in same orientation as activity A. If you deliberately specify the screen orientation of Activity B to Landscape , it will change the activity A also to landscape mode.

i suggest to make Activity B full activity.

于 2013-07-17T12:09:57.250 回答
0

onConfigurationChanges()在您的活动中覆盖方法。希望它可以帮助你。

并将其放在该活动下方的清单中android:configChanges="screenLayout|screenSize|orientation"

于 2013-07-17T10:49:08.810 回答
0

你试过吗---->

    <activity
        android:name="com.example.activityorientation.DialogAct"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Dialog"
        android:screenOrientation="fullsensor"/>
于 2013-07-17T11:26:29.140 回答
0

将此代码放入您的 OnCreate() 中。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

OnCreate() 方法示例(工作):

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        **setUIComponents();**
}
public void setUIComponents() {
        setContentView(R.layout.activity_ccap);
        **setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);**
        firstName = (EditText) findViewById(R.id.firstNameEditText);
        middleName = (EditText) findViewById(R.id.editText2);
        lastName = (EditText) findViewById(R.id.editText1);
        bussinessName = (EditText) findViewById(R.id.bussinessNameEditText);
        DOB = (EditText) findViewById(R.id.DOB);
        updateTextView = (TextView) findViewById(R.id.textView1);
        this.countyNumber = (Spinner) findViewById(R.id.countySpinner);
        this.numberPerPage = (Spinner) findViewById(R.id.itemsPerPageSpinner);
        popSpinner();
        submit = (Button) findViewById(R.id.button1);
        onSubmit();
    }

上面的代码在我的活动中工作;在您想保持“纵向”的任何活动中尝试此代码。此外,如果您想要不同的方向,只需更改ActivityInfo.SCREEN_ORIENTATION_PORTRAIT为其他内容,例如ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

特别适合您,这应该有效:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

编辑

这应该有效:

package com.example.activityorientation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
**setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);**

        Intent it = new Intent(getApplicationContext(),DialogAct.class);
        startActivity(it);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

现在在活动 B:

package com.example.activityorientation;

import android.app.Activity;
import android.os.Bundle;

public class DialogAct extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dailogact);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
}
于 2013-07-17T17:57:43.697 回答
0

尝试android:screenOrientation="sensorPortrait|sensorLandscape"Activity B中使用。

于 2013-07-17T11:27:01.750 回答
0

我有同样的问题,它对我有帮助:为 Activity B 添加 AndroidManifest

android:screenOrientation="fullSensor"
于 2017-04-17T08:37:27.017 回答
-1

AndroidManifest.xml在活动 B中放入以下行。

 android:configChanges="orientation" 

它将在两个方向上工作。

或者

检查您的移动设置或检查您的设备是否有动作。

于 2013-07-17T10:35:20.867 回答