微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ImageView/Shape Drawable 不能横向旋转

如何解决ImageView/Shape Drawable 不能横向旋转

我正在使用一个图像视图,其源指向我的应用程序背景的可绘制形状。我希望它在布局预览中看起来像这样(顶部为蓝色,底部为橙色)。

enter image description here

然而,每当我运行它时,它看起来像这样。它基本上是纵向版本,所以它没有被旋转。我在清单的活动标签中使用 android:screenorientation="landscape" 来强制它进入横向。

enter image description here

这是我的布局中图像视图的代码,以及 src

   <ImageView
    android:id="@+id/background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:importantForAccessibility="no"
    android:scaleType="centerCrop"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/background" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerColor="@color/light_blue_background"
        android:endColor="@color/orange_background"
        android:startColor="@color/teal_background" />
    <size android:height="16dp" />
    <size android:width="32dp" />
</shape>

我曾尝试在布局中使用旋转属性将其设置为 90 度,但效果不佳。

enter image description here

有人知道如何尝试让我的背景看起来像预览中应该的样子吗?

解决方法

 <ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no"
android:scaleType="centerCrop"
android:background="@drawable/background" />


<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <gradient
    android:centerColor="@color/light_blue_background"
    android:endColor="@color/orange_background"
    android:startColor="@color/teal_background" />

  </shape>
,

必须在形状可绘制 XML 本身中手动调整渐变角度。将角度设置为 90,交换开始和结束渐变颜色。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。