开发者

Android - Vertical layout only

开发者 https://www.devze.com 2022-12-23 18:21 出处:网络
How do I make sure my app is only for vertical layout? 开发者_开发技巧 I have tried android:screenOrientation=\"portrait\" but that doesn\'t seem to do the trick.You need to add to all your activity

How do I make sure my app is only for vertical layout?

开发者_开发技巧

I have tried android:screenOrientation="portrait" but that doesn't seem to do the trick.


You need to add to all your activity not for one only. I think you understood that setting is per application wide, but it isn't.

<activity android:name=".MyActivity"
          android:label="My Activity"
          android:screenOrientation="portrait">

Add the declaration to the activity tag in AndroidManifest for every Activity you want to be portrait-only.


If you want some group of your activities to be locked on PORTRAIT mode only, than you can choose the next way:

public abstract class BasePortraitActivity extends Activity {

    @Override
    protected final void onCreate(Bundle state) {
        super.onCreate(state);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        performOnCreate(state);
    }

    protected abstract void performOnCreate(Bundle state);

}

And than just extend BasePortraitActivity where you need it. Or just add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to YourActivity.onCreate().


you have to change into AndroidManifest.xml.

for each activity you have to insert:

android:configChanges = "orientation"

android:screenOrientation = "portrait"

for e.g.:

<activity android:name=".YourActivityName"
                  android:label="@string/app_name"
                  android:configChanges = "orientation"
                  android:screenOrientation = "portrait">

This works for a single activity.. But there seems no application wide setting there.


In your manifest under activity add this :

<activity
        android:name="com.zeus.MyProject"
        android:screenOrientation="portrait"
        >


Activity is to block just in case want to block all and only repeat this line of code in their Activitys.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Here, talk to the java does not want that the user can rotate their activity.

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or open your "AndroidManisfest.xml" and add the rows for the portrait mode as shown below.

        android:configChanges="orientation"
        android:screenOrientation="portrait">


Add android:configChanges="keyboardHidden|orientation" to the activity.


Put this attribute in layout root:

android:orientation="vertical"

It may help.


Just to confirm Pentium10's answer.
I was having a similar issue and adding android:screenOrientation="portrait" to the activity tag.
Did the trick for me.

0

精彩评论

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

关注公众号