从资源xml获取清单值

时间:2018-12-26 07:40:10

标签: android android-manifest android-resources

是否可以将清单中定义的值(例如“活动” screenOrientation)放置在资源xml文件中?

例如,“活动” screenOrientation属性可以具有“肖像”,“风景”等值。

好吧,这些值一经定义便是恒定的。 如果我希望手机和平板电脑具有不同的值,例如手机的“人像”,而在桌子上我希望它成为“传感器”,那该怎么办? 我不想在代码中设置它。

最后,这些值是整数常量,可以在这里找到: https://developer.android.com/reference/android/content/pm/ActivityInfo

因此,我想也许可以定义两个文件,分别是电话的integers.xml和大屏幕的integers-w400dp.xml,然后定义一个属性

<integer name="orientation">portrait</integer> //for phones in the integers.xml
<integer name="orientation">sensor</integer> //for tablets in the integers-w400dp.xml

然后我可以在清单中引用它们,就像这样:

android:screenOrientation="@integer/orientation" //This actually accepted with no error

但是,整数xml不会接受“纵向”或“传感器”值,而我必须将它们的数字值像这样放置:

<integer name="orientation">1</integer> //For portrait
<integer name="orientation">4</integer> //For sensors 

我相信这会起作用,但是我不想使用硬编码的值,因为它们可能会在api级别之间变化。

我试图将xmlns添加到资源文件(与清单中的ns相同),但是没有用。

*已编辑* 最重要的是,这个问题是关于如何在手机上将Activity锁定为人像,而不是在Tablet上锁定,而是通过xml而不是在代码中进行。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

  

这可能对您有帮助,请检查设备类型并设置活动方向   在运行时

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

public boolean isTablet() {
    return (this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}