我的目的是显示一个SVG背景图像填充整个屏幕的宽度,在ConstraintLayout
。 Button
叠加在这个背景图像上,这就是为什么你可以在下面的模型中看到:
背景图像是包含星星的图像。它的起点和终点将被限制在根GroupView
,因此它将完全填满屏幕的宽度。
问题如下:无论是否将底边绑定到屏幕的底边,背景图像都显示为distorded,如下所示:
这是我写的代码:
<ImageView
android:id="@+id/imageView16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/ic_background_stars"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/linearLayout4" // "linearLayout4" is just a widget shown above, it's not an important element for this StackOverflow question
/>
<Button
android:id="@+id/button_home_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:background="@color/colorRoyalRedLight"
android:text="TextView"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintStart_toStartOf="@+id/imageView16"
app:layout_constraintEnd_toEndOf="@+id/imageView16"
app:layout_constraintTop_toTopOf="@+id/imageView16"
app:layout_constraintBottom_toBottomOf="@+id/imageView16"
/>
我如何使用我的SVG图像作为背景图像,看起来没有任何扭曲,填满整个屏幕的宽度?高度当然可以自我调整(以保持良好的比例),但不应该比按钮更短(在Y和X中)。
对于ImageView
尝试设置android:scaleType="centerCrop"
。见ImageView.ScaleType。
CENTER_CROP 均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充)。然后图像在视图中居中。从XML,使用以下语法:android:scaleType =“centerCrop”。