我如何设置它,使应用程序只在纵向模式下运行?我希望在应用程序运行时禁用横向模式。如何通过编程实现呢?
当前回答
如果有人想知道,如何为整个应用程序做到这一点,而不必让所有活动都扩展Kotlin中的公共基类, 请看下面的例子:
class InteractiveStoryApplication: Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
override fun onActivityPaused(activity: Activity?) {
}
override fun onActivityResumed(activity: Activity?) {
}
override fun onActivityDestroyed(activity: Activity?) {
}
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
}
override fun onActivityStarted(activity: Activity?) {
}
override fun onActivityStopped(activity: Activity?) {
}
})
}
}
然后你必须像这样在AndroidManifest中添加你的公共基类:
<application android:allowBackup="true"
android:name=".InteractiveStoryApplication"
其他回答
来自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>
你可以在你的manifest文件中设置这个。
android:name=".your launching activity name"
android:screenOrientation="portrait"
你也可以通过在你的类文件中写这样的代码来达到同样的目的:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Use:
android:screenOrientation="portrait"
只需将这一行写在应用程序的清单文件中,在每个你想只显示在纵向模式的活动中。
类似于Graham Borland的答案……但是如果你不想的话,你似乎不需要创建Application类…只需在项目中创建一个Base Activity
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
并且在你想使用Potrait Mode的地方扩展这个类而不是AppCompatActivity
public class your_activity extends BaseActivity {}
将android:screenOrientation="portrait"添加到AndroidManifest.xml中的活动中。例如:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
推荐文章
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败