los*_*sit 910 android android-manifest android-orientation
如何在我的Android应用中禁用某些视图的横向模式?
Yon*_*lan 1586
添加android:screenOrientation="portrait"到AndroidManifest.xml中的活动.例如:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
Run Code Online (Sandbox Code Playgroud)
编辑:由于这已成为一个超级流行的答案,我感到非常内疚,因为强迫肖像很少是解决它经常应用的问题的正确方法.
强迫肖像的主要警告:
retainInstance碎片.因此,大多数应用应该让手机传感器,软件和物理配置自行决定用户如何与您的应用进行互动.但是,如果您对sensor用例中的方向的默认行为不满意,可能还需要考虑一些案例:
nosensor方向.大多数手机上的大多数平板电脑和人像都有这种风景,但我仍然不建议大多数"正常"的应用程序(有些用户喜欢在手机上键入横向软键盘,许多平板电脑用户都是纵向阅读 - 以及你应该让他们).sensorPortrait可能比portraitAndroid 2.3+ 更好; 这允许颠倒的肖像,这在平板电脑使用中非常常见.Ric*_*ich 105
AndroidManifest.xml在阅读这篇文章之前我不知道文件切换,所以在我的应用程序中我使用了它:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed Portrait orientation
Run Code Online (Sandbox Code Playgroud)
Dee*_*ami 40
android:screenOrientation="portrait"在清单文件中添加此项,您可以在此处声明您的活动
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait" />
Run Code Online (Sandbox Code Playgroud)
如果你想用java代码试试
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
在setContentView为您的活动调用方法之前onCreate().
希望这对所有人都有帮助并且易于理解......
Mik*_*eir 25
这里有很多答案建议"portrait"在你的AndroidManifest.xml文件中使用.这看起来似乎是一个很好的解决方案 - 但正如文档中所述,您正在挑选出可能只有景观的设备.您还强制某些设备(在横向上效果最佳)进入人像,而不是获得正确的方向.
我的建议是"nosensor"改用.这将使设备使用其默认首选方向,不会阻止在Google Play上进行任何购买/下载,并确保传感器不会弄乱您的(NDK,在我的情况下)游戏.
小智 12
如果你想要用户设置,
那我建议 setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
您可以从设置菜单更改设置.
我需要这个,因为我的计时器必须与屏幕上的内容相对应,旋转屏幕会破坏当前的活动.
Sun*_*ary 12
只需在你的清单中添加Like this Line
机器人:screenOrientation = "画像"
<manifest
package="com.example.speedtest"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="ComparisionActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
Bri*_*hod 12
您可以为整个应用程序执行此操作,而不必让所有活动都扩展一个公共基类。
诀窍是首先确保您在项目中包含一个 Application 子类。在您的应用程序首次启动时调用的 onCreate() 中,您注册一个 ActivityLifecycleCallbacks 对象(API 级别 14+)以接收活动生命周期事件的通知。
这使您有机会在应用程序中的任何活动开始(或停止、恢复或其他任何活动)时执行自己的代码。此时,您可以对新创建的活动调用 setRequestedOrientation()。
并且不要忘记在清单文件中添加 app:name=".MyApp"。
class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// register to be informed of activities starting up
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity,
Bundle savedInstanceState) {
// new activity created; force its orientation to portrait
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
....
});
}
}
Run Code Online (Sandbox Code Playgroud)
在Activity的onCreate()中使用它
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
将其放入您的清单中。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortrait" />
Run Code Online (Sandbox Code Playgroud)
方向将为纵向,但如果用户的手机倒置,它也会显示正确的方向。(所以你的屏幕将旋转 180 度。)
如果活动在多窗口模式下运行,系统将忽略此属性。
更多: https: //developer.android.com/guide/topics/manifest/activity-element
如果您不想更好地处理在每个清单活动条目中添加方向的麻烦,请创建一个BaseActivity类(继承'Activity'或'AppCompatActivity'),它将由应用程序的每个活动继承,而不是'Activity '或'AppCompatActivity',只需在BaseActivity中添加以下代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// rest of your code......
}
Run Code Online (Sandbox Code Playgroud)
用:
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait"
Run Code Online (Sandbox Code Playgroud)
您必须设置每个活动的方向。
<activity
android:name="com.example.SplashScreen2"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.Registration"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.Verification"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.WelcomeAlmostDone"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name="com.example.PasswordRegistration"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
Run Code Online (Sandbox Code Playgroud)
如果您要禁用Landscape mode for your android app(或一项活动),只需添加即可,
android:screenOrientation="portrait"到AndroidManifest.xml文件中的活动标签。
喜欢:
<activity android:name="YourActivityName"
android:icon="@drawable/ic_launcher"
android:label="Your App Name"
android:screenOrientation="portrait">
Run Code Online (Sandbox Code Playgroud)
另一种方式,程序化方法。
如果您想以编程方式执行此操作,即。使用Java代码。您可以通过以下方式将代码添加到您不想在横向模式下显示的活动的Java类中。
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助。有关更多详细信息,请访问此处,在此处输入链接说明
小智 5
如果您使用的是Xamarin C#,其中一些解决方案将不起作用。这是我发现有效的解决方案。
[Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
Run Code Online (Sandbox Code Playgroud)
以上类效果很好,类似于其他解决方案。此外,它不是全局适用的,需要放置在每个活动标题中。
如何在某些视图中更改方向
除了锁定整个活动的方向,您可以使用此类从实用的角度动态锁定任何视图的方向:-
使您的风景
OrientationUtils.lockOrientationLandscape(mActivity);
Run Code Online (Sandbox Code Playgroud)
使您的视图人像
OrientationUtils.lockOrientationPortrait(mActivity);
Run Code Online (Sandbox Code Playgroud)
解锁方向
OrientationUtils.unlockOrientation(mActivity);
Run Code Online (Sandbox Code Playgroud)
方向实用程序类
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Surface;
import android.view.WindowManager;
/* * This class is used to lock orientation of android app in nay android devices
*/
public class OrientationUtils {
private OrientationUtils() {
}
/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
/** Locks the device window in portrait mode. */
public static void lockOrientationPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/** Locks the device window in actual screen mode. */
public static void lockOrientation(Activity activity) {
final int orientation = activity.getResources().getConfiguration().orientation;
final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
.getRotation();
// Copied from Android docs, since we don't have these values in Froyo
// 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
// Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
if (!(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)) {
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}
/** Unlocks the device window in user defined screen mode. */
public static void unlockOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
555964 次 |
| 最近记录: |