我正在制作一個應用程式并希望影像可以在不同尺寸的螢屏上很好地縮放,但是當我使寬度和高度與父級匹配時,它忽略了邊距,使影像在較小的設備中越界而在較大的。我發現的修復在下面的答案中:
uj5u.com熱心網友回復:
我喜歡讓我的影像四舍五入,所以我把它們放在 CardView 中。它看起來像這樣:
<androidx.cardview.widget.CardView
android:id="@ id/cardView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:adjustViewBounds="true"
android:layout_marginRight="32dp" <-- your margins
android:layout_marginLeft="32dp" <-- your margins
app:cardCornerRadius="20dp" <-- your rounded corners radius
app:cardBackgroundColor="@color/background" <-- your background color
app:layout_constraintTop_toBottomOf="@ id/top" <-- the view you have on top of the image. If there's nothing on top, set it to "parent" or skip it altogether.
app:layout_constraintBottom_toTopOf="@id/bottom" <-- the view you have under the image. If there's nothing on top, set it to "parent" or skip it altogether.
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="@ id/imageView"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"/>
</androidx.cardview.widget.CardView>
如果您不喜歡四舍五入的影像,請跳過 CardView:
<ImageView
android:id="@ id/imageView"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:adjustViewBounds="true"
android:layout_marginRight="32dp" <-- your margins
android:layout_marginLeft="32dp" <-- your margins
android:scaleType="centerCrop"
app:layout_constraintTop_toBottomOf="@ id/eachText"<-- the view you have on top of the image. If there's nothing on top, set it to "parent" or skip it altogether.
app:layout_constraintBottom_toTopOf="@id/bottom" <-- the view you have under the image. If there's nothing on top, set it to "parent" or skip it altogether.
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
uj5u.com熱心網友回復:
您應該將其添加到 xml 檔案中的 imageView 中
android:scaleType="fitXY"
如果它不起作用,請更改“fitXY”以保留 scaleType 的選項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/395906.html
上一篇:添加按鈕以動態查看現有視圖
