开发者

Android screenorientation change for all activities

I want to fix screen orientation on开发者_如何转开发 my android application to portrait. Using google, i've found, that this can be done by adding the following code to my 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. Is there any way to apply orientation to all activities at once?

I tried to use styles in a following way:

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

<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. 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.


You can create a class which extends 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.

EDIT : SCREEN_ORIENTATION_PORTRAIT or SCREEN_ORIENTATION_LANDSCAPE

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜