我如何设置它,使应用程序只在纵向模式下运行?我希望在应用程序运行时禁用横向模式。如何通过编程实现呢?
当前回答
有两种方法。
添加android:screenOrientation="portrait"在你的manifest文件 相应的活动 添加 setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 在onCreate()方法中添加到您的活动
其他回答
类似于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 {}
你可以在你的manifest文件中设置这个。
android:name=".your launching activity name"
android:screenOrientation="portrait"
你也可以通过在你的类文件中写这样的代码来达到同样的目的:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
是的,您既可以通过编程方式做到这一点,也可以为您的所有活动创建一个所有活动都可以扩展的AbstractActivity。
public abstract class AbstractActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
这个抽象活动也可以用于全局菜单。
在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>
推荐文章
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?