在纵向和横向之间切换时,Android整数xml资源值不会更改

San*_*aza 7 android landscape-portrait android-resources

我使用以下GradientDrawable作为RelativeLayout的背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:angle="@integer/my_value"
        android:endColor="#0FFF"
        android:startColor="#FFFF" />
</shape>
Run Code Online (Sandbox Code Playgroud)

然后在res\values文件夹中创建一个名为integers.xml的文件:

<resources>
    <integer name="my_value">90</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

另一个在res\values-land文件夹下具有相同名称的文件:

<resources>
    <integer name="my_value">180</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

因此,当我旋转设备时,梯度从运行Android 2.3.3(如预期)时从底部 - >从上到右 - >左,但是当我在Android 4.4.4上测试时,渐变角度不会改变.

我也尝试过:

<item name="my_value" format="float" type="dimen">90</integer>
Run Code Online (Sandbox Code Playgroud)

但结果相同.

有任何想法吗?

更新:

使用GradientDrawable的RelativeLayout存在于2个不同的文件中:

  1. 水库\布局\ my_layout
  2. 水库\布局,土地\ my_layout

水库\布局,土地\ my_layout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/my_drawable" >
Run Code Online (Sandbox Code Playgroud)

更新2:

只有在旋转设备时才会出现问题.如果我使用先前旋转的设备启动活动,则正确设置渐变角度.

Sim*_*uis 1

一个快速的解决方法是直接使用两种不同的形状:

  1. 一种用于肖像模式

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <gradient
            android:angle="90"
            android:endColor="#0FFF"
            android:startColor="#FFFF" />
    </shape>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 一种用于横向模式

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <gradient
            android:angle="180"
            android:endColor="#0FFF"
            android:startColor="#FFFF" />
    </shape>
    
    Run Code Online (Sandbox Code Playgroud)