Android 在安卓系统中,如何设置背景图像并使其适合整个卡的卡视图?
在这里,我试图适应整个图像作为背景卡的看法。但它需要一些空间,如图所示。我尝试了android:scaleType=“centerCrop”和Android 在安卓系统中,如何设置背景图像并使其适合整个卡的卡视图?,android,android-layout,android-imageview,android-xml,android-cardview,Android,Android Layout,Android Imageview,Android Xml,Android Cardview,在这里,我试图适应整个图像作为背景卡的看法。但它需要一些空间,如图所示。我尝试了android:scaleType=“centerCrop”和fitXY等,但它没有响应。在所附图像中,紫色表示占用的空白空间。我需要空间需要被背景图像占据。这是我的密码 <?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="h
fitXY
等,但它没有响应。在所附图像中,紫色表示占用的空白空间。我需要空间需要被背景图像占据。这是我的密码
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/voilet">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/application_ads_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="@drawable/app_ads_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/application_ads_install_button"
android:layout_width="74dp"
android:layout_height="34dp"
android:layout_marginEnd="48dp"
android:background="@color/voilet"
android:text="@string/application_ads_install"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/application_ads_image"
app:layout_constraintTop_toTopOf="@+id/application_ads_image"
app:layout_constraintVertical_bias="0.674" />
<TextView
android:id="@+id/application_ads_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/application_ads_review"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/application_ads_image"
app:layout_constraintEnd_toStartOf="@+id/application_ads_install_button"
app:layout_constraintHorizontal_bias="0.27"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/application_ads_name"
app:layout_constraintVertical_bias="0.567" />
<TextView
android:id="@+id/application_ads_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Zira"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="@+id/application_ads_image"
app:layout_constraintHorizontal_bias="0.176"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
在ConstraintLayout
中,如果要使宽度和高度适合家长,有两个选项
1.在layout\u约束
attrs中,您需要在layout\u宽度
和layout\u高度
<ImageView
android:id="@+id/application_ads_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
更新
我给你写了一个样本,你可以这样检查结果
我尝试使用带有ImageView fitXY或matrix scaleType的本地资源可绘制图像处理布局文件,ImageView图片完全适合卡视图。类似于下面的内容
<ImageView
android:id="@+id/application_ads_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/a1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
a1是我的本地可绘制图像文件@drawable/a1。
如果您仍然有问题,我建议您检查图像/图标尺寸,或检查您使用此布局卡的父视图。您可以使用android:background=“@drawable/app\u ads\u background”设置cardview背景图像,谢谢您的回复。图像不能设置为卡片视图的背景图像。尽管我按照你说的做了更改,但它不起作用。谢谢你的回复,你能再解释一下吗。解释上有点混乱。在ConstraintLayout
中,如果希望视图具有宽度(或高度)wrap\u内容
,则需要使用layout\u约束约束
,或者如果使用匹配父对象
,则不需要约束任何内容。这些功能取决于ConstraintLayout
FYI,我通常会添加layout\u constraintStart
和layout\u constraintEnd
,并将0dp
设置为layout\u width
以水平匹配父项。感谢您的回复,layout\u constraintStart和layout\u constraintEnd中要添加的值是什么?对不起,layout\u constraintStart/End
并不是一个真正的属性,它只是缩写,意思是StartToStartOf、StartToEndOf、EndToStartOf、EndToEndOf。。等等,我们约束的值是parent
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/voilet">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/application_ads_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/app_ads_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/application_ads_install_button"
android:layout_width="74dp"
android:layout_height="34dp"
android:layout_margin="4dp"
android:background="@color/voilet"
android:text="INSTALL"
android:textColor="#FFF"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/application_ads_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginStart="16dp"
android:text="5/5"
android:textColor="#FFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/application_ads_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Zira"
android:textColor="#FFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<ImageView
android:id="@+id/application_ads_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/a1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />