Nat*_*amy 6 android android-layout android-xml android-imageview android-cardview
在这里,我试图将整个图像作为卡片视图的背景。但它需要一些空间,如图所示。我试过android:scaleType="centerCrop"
和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>
Run Code Online (Sandbox Code Playgroud)
在 中ConstraintLayout
,如果您想让宽度和高度适合父母,有两种选择。
1.在layout_constraint
attrs中,需要设置0dp
on layout_width
&layout_height
<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" />
Run Code Online (Sandbox Code Playgroud)
2.如果设置了match_parent
,则不需要设置layout_constraint
。如果设置的话,效果就像wrap_content
<ImageView
android:id="@+id/application_ads_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"/>
Run Code Online (Sandbox Code Playgroud)
我给你写了一个示例,你可以这样检查结果
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
624 次 |
最近记录: |