【问题标题】:Change manifest screenOrientation with values使用值更改清单屏幕方向
【发布时间】:2014-02-28 20:14:09
【问题描述】:

我正在训练通过资源使用常量值更改清单中的 screenOrientation。这是我的清单的一个活动:

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

我想用这个常量http://developer.android.com/reference/android/R.attr.html#screenOrientation、10 英寸横向、7 英寸横向、智能手机纵向更改我的 screenOrientation,所以我在 3 个不同的目录中创建了资源 xml:

值/整数.xml

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

值-大/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>

但在所有设备中,应用始终是纵向的。我错了什么? 谢谢你的帮助。

M

【问题讨论】:

标签: android xml orientation manifest


【解决方案1】:

您可以检测屏幕尺寸并以编程方式设置方向。 这是一个例子:

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);
    }
}

希望对你有帮助。

【讨论】:

  • Tnx,我会试试的。我更喜欢 xml 解决方案,但它也应该可以工作。再次感谢您。
  • 请注意,这可能会开始您的活动两次。看到这个帖子:stackoverflow.com/questions/26563797/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
相关资源
最近更新 更多