Change manifest screenOrientation with values

你说的曾经没有我的故事 提交于 2020-01-04 02:35:08

问题


I'm training to change the screenOrientation in manifest with constant values via resources. This is an activity of my manifest:

<activity
    android:name="it.wrapmobile.parcosigurta.NavActivity"
    android:label="@string/app_name"
    android:screenOrientation="@integer/orientation" >
</activity>

i want to change my screenOrientation with this constants http://developer.android.com/reference/android/R.attr.html#screenOrientation , 10inch landscape, 7inch landscape, smartphonne portrait so i created resources xml in 3 different directory:

values/integer.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">1</integer>
</resources>

values-large/integer.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">0</integer>
</resources>

values-sw600dp/integer.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">0</integer>
</resources>

But in all devices the app is always in portrait. What i wrong? Thank you for you help.

M


回答1:


You could detect the screen size and set the orientation programmatically. Here is an example:

public static void setActivityScreenOrientation(Activity act)
{
    boolean isTablet = false;

    if ((act.getResources().getConfiguration().screenLayout 
            & android.content.res.Configuration.SCREENLAYOUT_SIZE_MASK) == android.content.res.Configuration.SCREENLAYOUT_SIZE_LARGE)
    {
        isTablet = true;
    }

    if ((act.getResources().getConfiguration().screenLayout 
            & android.content.res.Configuration.SCREENLAYOUT_SIZE_MASK) == android.content.res.Configuration.SCREENLAYOUT_SIZE_XLARGE)
    {
        isTablet = true;
    }


    if (isTablet)
    {
        act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }else
    {
        act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

Hope it helps.



来源:https://stackoverflow.com/questions/22104958/change-manifest-screenorientation-with-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!