你好,我有一张640X480的球型图,在图片中,你可以看到我把layout_width指定为849px,layout_height指定为680px。有一张640X480的球型图片,在图片中,你可以看到我指定layout_width为849px,layout_height为680px,我取了一个蓝色的背景色,由于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>
你的情况是由于图片没有大到 ImageView
是。如果你想让图像完全填充在 View
你可以使用属性 android:scaleType="centerCrop"
. 你可以跟着一起去 本视觉指南 在ImageView的不同比例类型上,看看它在不同比例类型上的表现。
如果你想在图像上有一个边框,那么你可以使用 ImageView
我不会使用 android:background
属性,因为你的图像将被绘制在背景之上,可能会完全覆盖它。
我的方法是有一个比图像视图大一点的父视图,由该视图来保存背景属性。你可以通过一个 FrameLayout
并应用一些 android:layout_margin
至 ImageView
像下面的例子。
<?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">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2196F3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView3"
android:gravity="center"
android:layout_margin="16dp"
android:layout_width="849px"
android:layout_height="680px"
app:srcCompat="@drawable/ball_640x480" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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:srcCompat="@drawable/ball_640x480"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>