我如何设置它,使应用程序只在纵向模式下运行?我希望在应用程序运行时禁用横向模式。如何通过编程实现呢?
当前回答
在Manifest文件中,你想在“肖像”中使用哪些活动,你必须在活动标签中写这些代码
android:screenOrientation="portrait"
像这样
android:图标= " @drawable /图标” android: name = " com.zemkoapps.hd.wallpaper.AndroidGridLayoutActivity " android: screenOrientation = "肖像" > 但如果你想在横屏中使用这段代码 android: screenOrientation = "风景"
其他回答
来自Android开发者指南:
"orientation" The screen orientation has changed — the user has rotated the device. Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations. "screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device). Added in API level 13.
所以,在AndroidManifest.xml文件中,我们可以放:
<activity
android:name=".activities.role_activity.GeneralViewPagerActivity"
android:label="@string/title_activity_general_view_pager"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
>
</activity>
在kotlin——>
在你的Extends Application类中使用这个onCreate()…
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
p0.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
}
override fun onActivityStarted(p0: Activity) {
}
override fun onActivityResumed(p0: Activity) {
}
override fun onActivityPaused(p0: Activity) {
}
override fun onActivityStopped(p0: Activity) {
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
}
override fun onActivityDestroyed(p0: Activity) {
}
}
)}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting screen orientation locked so it will be acting as potrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
适用于任何Android版本
从XML
你可以在manifest.xml文件中为每个活动指定android:screenOrientation="portrait"。不能在应用程序标记上指定此选项。
从Java
另一个选择是通过编程来实现,例如在Activity基类中:
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Android 4+ (API 14+)
最后一个选项是使用活动生命周期监听器,这只在Android 4.0 (API 14+)后可用。所有事情都发生在一个自定义Application类中:
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleAdapter() {
@Override
public void onActivityCreated(Activity a, Bundle savedInstanceState) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
}
ActivityLifecycleAdapter只是一个您需要创建的助手类,它将是ActivityLifecycleCallbacks的一个空实现(因此当您只需要其中一个方法时,您不必重写该接口的每个方法)。
对于Xamarin用户:
如果你把你所有的活动扩展到一个BaseActivity,只需添加:
this.RequestedOrientation = ScreenOrientation.Portrait;
这样问题就解决了。如果你想要任何特定的活动在横向覆盖OnActivityCreated。为:
this.Activity.RequestedOrientation = ScreenOrientation.Landscape;
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件