设置机器人活动的屏幕方向与values​​.xml机器人、屏幕、方向、xml

2023-09-07 12:59:12 作者:入戏太深、爱你太真

我想设置活动屏幕方向与来自RES /值的XML文件中的值。我想这样做,因为,或多或少,我需要同样的活动为平板电脑(横向)和智能手机(纵向)。

I am trying to setup activity screen orientation with values from XML file in res/values. I would like to do that because, more or less, I need same Activity for both tablet (landscape) and smartphone (portrait).

清单:

<activity android:name="..." android:screenOrientation="@string/defaultOrientation"/>

config.xml文件:

config.xml:

<string name="defaultOrientation">portrait</string>

不过,在此设置的应用程序将不会出现在设备上,它会返回该错误:

But with this setting application won't appear on device and it will return this error:

java.lang.NumberFormatException:无效INT:人像

java.lang.NumberFormatException: Invalid int: "portrait"

好了,所以我干脆改成了这个

Second

OK, so I simply changed it to this

清单:

<activity android:name="..." android:screenOrientation="@integer/defaultOrientation"/>

config.xml文件:

config.xml:

<integer name="defaultOrientation">1</integer>

我用1因为ActivityInfo.SCREEN_ORIENTATION_PORTRAIT == 1。

I used 1 because ActivityInfo.SCREEN_ORIENTATION_PORTRAIT == 1.

不过,这是行不通的两种。看来,我可能会修改一些值,例如应用程序/活动名称而不是屏幕方向?

But this is not working either. It seems that I may modify some values like application / activity name but not screen orientation ?

我知道,我可以用code解决方法,但由于某种原因,它认为这也应该是由XML值的文件得到。

I know that I may workaround it by code but for some reason it feels that this also should be obtainable by XML values file.

这是某种可以通过XML值实现的呢?

It is somehow possible to achieve it by XML values ?

推荐答案

我同样的问题,你的第二个exlanation我用一种变通方法由code,你是不是在找。

Same problem for me with your second exlanation and I used a workaround by code which you aren't looking for.

我加了4个值的文件夹下的资源文件夹中。 价值,价值观-V11,价值观V14和价值观sw720dp

I added 4 values folders under res folder. "values", "values-v11", "values-v14" and "values-sw720dp"

所有值文件夹有integers.xml。

All values folders have "integers.xml".

价值观和价值观V14都值1,它是纵向; &LT;整数NAME =portrait_if_not_tablet&GT; 1&LT; /整数GT;

"values" and "values-v14" have value 1 which is portrait orientation; <integer name="portrait_if_not_tablet">1</integer>.

的价值观-V11和价值观sw720dp有值2,这是用户的方向; &LT;整数NAME =portrait_if_not_tablet&GT; 2'; /整数GT;

"values-v11" and "values-sw720dp" have value 2 which is user orientation; <integer name="portrait_if_not_tablet">2</integer>.

和清单文件,活动有一个属性等等; 机器人:screenOrientation =@整数/ portrait_if_not_tablet

And in Manifest file, activity has a property like; android:screenOrientation="@integer/portrait_if_not_tablet".

所有价值,价值观-V11,价值观V14是否按预期工作,但价值观sw720dp!

在调试,我意识到portrait_if_not_tablet之价值之际,预计sw720dp设备(与API 16)与getResources()。getInteger(R.integer.portrait_if_not_tablet),但是当我通过getRequestedOrientation检查电流方向的值()我得到了不同的值。

While debugging I realized that value of portrait_if_not_tablet comes as expected on a sw720dp device(with API 16) with getResources().getInteger(R.integer.portrait_if_not_tablet) but when i checked the value of current orientation by getRequestedOrientation() I got a different value.

int requestedOrientation = getResources().getInteger(R.integer.portrait_if_not_tablet);
int currentOrientation = getRequestedOrientation();
if (currentOrientation != requestedOrientation) {
    setRequestedOrientation(requestedOrientation);
}

所以,我用我的活动的onCreate方法的code座解决这个问题。

So I used a code block on onCreate method of my activities to solve this.

 
精彩推荐
图片推荐