简体   繁体   English

Android屏幕方向更改所有活动

[英]Android screenorientation change for all activities

I want to fix screen orientation on my android application to portrait. 我想在我的Android应用程序上修改屏幕方向为肖像。 Using google, i've found, that this can be done by adding the following code to my manifest.xml: 我发现,使用谷歌可以通过将以下代码添加到我的manifest.xml来完成:

<activity android:name=".LauncherActivity"
  android:configChanges="keyboardHidden|orientation" 
  android:screenOrientation="portrait">
</activity>

That's fine enough, but the problem is follows: I've about 15 activities now, and this number will grow. 这很好,但问题如下:我现在大约有15项活动,这个数字会增长。 Is there any way to apply orientation to all activities at once? 有没有办法立即对所有活动进行定向?

I tried to use styles in a following way: 我尝试以下列方式使用样式:

theme.xml theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Custom" parent="android:Theme">
    <item name="android:screenOrientation">portrait</item>
    <item name="android:windowBackground">@drawable/launcher</item>
    <item name="android:configChanges">keyboardHidden|orientation</item>
  </style>
</resources>

Manifest.xml 的Manifest.xml

<application ... 
android:debuggable="true"
  android:theme="@style/Custom"
>
//...
</application>

or 要么

<activity
... 
  android:theme="@style/Custom"
>
//...
</activity>

Both the examples apply windowBackground succesfully, but screenOrientation does't works. 两个示例windowBackground成功应用了windowBackground ,但screenOrientation windowBackground Any ideas? 有任何想法吗?

No way. 没门。 I need to repeat 我需要重复一遍

<activity android:name=".LauncherActivity"
  android:configChanges="keyboardHidden|orientation" 
  android:screenOrientation="portrait">
</activity>

in every <activity> tag in manifest. 在清单中的每个<activity>标记中。

You can create a class which extends Activity : 您可以创建一个扩展Activity的类:

public class PortraitActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

And I think that if your activities extends PortraitActivity instead of Activity it will do. 而且我认为如果你的活动扩展了PortraitActivity而不是Activity那么它就可以。

EDIT : SCREEN_ORIENTATION_PORTRAIT or SCREEN_ORIENTATION_LANDSCAPE 编辑:SCREEN_ORIENTATION_PORTRAIT或SCREEN_ORIENTATION_LANDSCAPE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM