【发布时间】:2020-06-14 20:53:37
【问题描述】:
你好。有一个 640X480 的球图像。在图像中,如您所见,我将 layout_width 指定为 849px,layout_height 指定为 680 px。我采用了蓝色的背景色。由于指定的 layout_width 远大于 640px(图像的宽度大小),那么为什么图片的左右两边没有被蓝色背景包围。图片的顶部和底部被蓝色背景包围。
XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView3"
android:layout_width="849px"
android:layout_height="680px"
android:background="#2196F3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ball_640x480" />
</androidx.constraintlayout.widget.ConstraintLayout>
我无法理解的是在图像中,设置 wrap_content 后为什么图像上下仍然有空白?
以下是更新后的xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ball_640x480" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
请贴出 XML 代码,方便调试
-
我已经添加了 XML 代码。
标签: android android-studio android-layout imageview pixel