3 回答

TA貢獻1868條經(jīng)驗 獲得超4個贊
圖像視圖不會自動調(diào)整大小
因為您使用的是固定大小值,所以imageView
您遇到了以下問題:
不同的手機有不同的屏幕尺寸,在您的布局中,您在視圖上使用固定尺寸(240dp
例如固定尺寸),結(jié)果是在一個屏幕(您的 android studio 預(yù)覽屏幕)上看起來不錯的內(nèi)容在另一個屏幕上看起來不太好屏幕(您的實際手機)。
怎么修
您可以使用這些屬性來使圖像的大小響應(yīng):
app:layout_constraintHeight_percent="0.25" app:layout_constraintWidth_percent="0.25"
您需要0dp
在android:layout_width
和 中給出圖像尺寸android:layout_height
我所做的是根據(jù)屏幕尺寸告訴視圖的寬度和高度相等25%
,這將使您的視圖響應(yīng)所有屏幕尺寸,因此它將“自動調(diào)整大小”

TA貢獻1790條經(jīng)驗 獲得超9個贊
更新
根據(jù)@tamir-abutbul的回答,使用layout_constraintHeight_percent和layout_constraintWidth_percent使視圖根據(jù)屏幕尺寸適合是一個很好的解決方法。我已將頂部項目放置在網(wǎng)格布局中,您可以將其應(yīng)用于您正在使用的任何頂部布局,主要要考慮的是此處的layout_constraintHeight/Width_percent。另外,對于圖像,我已將背景設(shè)置為透明并將scaleType更改為“fitCenter”,以便圖像在任何屏幕上保持其縱橫比。希望這可以幫助。您可以將 android 更改為 androidx。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="0dp"
android:background="#faf"
android:columnCount="5"
android:rowCount="4"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintTop_toTopOf="parent">
</GridLayout>
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:background="#00000000"
android:scaleType="fitCenter"
android:src="@drawable/lamborghini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.30"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gridLayout"
app:layout_constraintVertical_bias="0.265"
app:layout_constraintWidth_percent="0.7" />
<Button
android:id="@+id/resetButton"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:background="#F70000"
android:text="Undo"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:layout_constraintVertical_bias="0.395" />
</android.support.constraint.ConstraintLayout>

TA貢獻2019條經(jīng)驗 獲得超9個贊