Setting Android Activity screen orientation with values.xml

前端 未结 1 977
名媛妹妹
名媛妹妹 2021-01-18 20:11

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

相关标签:
1条回答
  • 2021-01-18 20:48

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

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

    All values folders have "integers.xml".

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

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

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

    All "values", "values-v11", "values-v14" are working as expected but "values-sw720dp"!

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

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

    0 讨论(0)
提交回复
热议问题