我如何设置它,使应用程序只在纵向模式下运行?我希望在应用程序运行时禁用横向模式。如何通过编程实现呢?


当前回答

在Manifest文件中,你想在“肖像”中使用哪些活动,你必须在活动标签中写这些代码

  android:screenOrientation="portrait" 

像这样

android:图标= " @drawable /图标” android: name = " com.zemkoapps.hd.wallpaper.AndroidGridLayoutActivity " android: screenOrientation = "肖像" > 但如果你想在横屏中使用这段代码 android: screenOrientation = "风景"

其他回答

在config.xml中的<widget>下添加<preference name="orientation" value="portrait" />对我来说很有效。

(其他解决方案要么在我的设备上不起作用,要么在构建过程中被覆盖,要么在构建过程中给出了弃用错误。)

在Manifest文件中,你想在“肖像”中使用哪些活动,你必须在活动标签中写这些代码

  android:screenOrientation="portrait" 

像这样

android:图标= " @drawable /图标” android: name = " com.zemkoapps.hd.wallpaper.AndroidGridLayoutActivity " android: screenOrientation = "肖像" > 但如果你想在横屏中使用这段代码 android: screenOrientation = "风景"

对于Xamarin用户:

如果你把你所有的活动扩展到一个BaseActivity,只需添加:

this.RequestedOrientation = ScreenOrientation.Portrait;

这样问题就解决了。如果你想要任何特定的活动在横向覆盖OnActivityCreated。为:

this.Activity.RequestedOrientation = ScreenOrientation.Landscape;

类似于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 {}
@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);
}