如何auto-resize imageview for different screen sizes android studio?
How to auto-resize imageview for different screen sizes android studio?
我开始使用 Android Studio 为 android 设备开发应用程序。当我测试我的应用程序的屏幕尺寸兼容性时,我注意到图像视图没有 auto-resize。
imageview 的位置在不同的屏幕尺寸下看起来完全不同。如果屏幕足够小,imageview 有时会放在 viewscope 之外。
我在整个互联网上都看过了,但我对如何使应用程序与大多数屏幕尺寸(不包括平板电脑)兼容感到有点困惑。我去了 android 网站,他们说要使用 wrap_content。但是如果我使用它,我将无法根据我的需要调整图像视图的高度和宽度。
有谁知道如何根据屏幕尺寸制作图像视图auto-resize?
以下是正在发生的事情的图像:
这是应用程序的布局:
这就是我想要的样子(Pixel 3):
但这是它在较小屏幕 (Nexus 5) 中的样子:
在 Xcode 中有一个名为 auto-resize 的功能可以自动为您调整内容的大小。 android工作室有类似的东西吗?
这是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">
<Button
android:id="@+id/resetButton"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginBottom="56dp"
android:background="#F70000"
android:text="Undo"
android:textColor="#000000"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="288dp"
android:layout_height="248dp"
android:background="#1750DF"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.685" />
</androidx.constraintlayout.widget.ConstraintLayout>
提前致谢!
更新
根据@tamir-abutbul 的回答,使用 layout_constraintHeight_percent 和 layout_constraintWidth_percent 使视图适合屏幕大小是一个很好的解决方法。
我已将顶部项目放置在网格布局中,您可以将其应用于您正在使用的任何顶部布局,主要要考虑的是此处的 layout_constraintHeight/Width_percent。
此外,对于图像,我已将背景设置为透明并将 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>
你的图像视图是这样的,因为你在其他视图中定义的宽度和高度很大,所以也许你可以在其他屏幕上尝试使用这个库来获得相同的尺寸
图像视图不会自动调整大小
因为您在 imageView
上使用固定尺寸值,所以您遇到了这个问题:
不同的 phone 有不同的屏幕尺寸,在您的布局中,您在视图上使用固定尺寸(例如,固定尺寸为 240dp
)结果是,在一个屏幕(您的 android 工作室预览屏幕)上可能看起来不错的内容在另一个屏幕(您的实际 phone)上可能看起来不太好。
如何修复
您可以使用这些属性使您的图像尺寸响应:
app:layout_constraintHeight_percent="0.25"
app:layout_constraintWidth_percent="0.25"
您需要在 android:layout_width
和 android:layout_height
中为您的图片提供 0dp
大小
我所做的是根据屏幕尺寸告诉视图在宽度和高度上都等于 25%
,这将使您的视图响应所有屏幕尺寸,因此它将 "auto-resize"
我开始使用 Android Studio 为 android 设备开发应用程序。当我测试我的应用程序的屏幕尺寸兼容性时,我注意到图像视图没有 auto-resize。
imageview 的位置在不同的屏幕尺寸下看起来完全不同。如果屏幕足够小,imageview 有时会放在 viewscope 之外。
我在整个互联网上都看过了,但我对如何使应用程序与大多数屏幕尺寸(不包括平板电脑)兼容感到有点困惑。我去了 android 网站,他们说要使用 wrap_content。但是如果我使用它,我将无法根据我的需要调整图像视图的高度和宽度。
有谁知道如何根据屏幕尺寸制作图像视图auto-resize?
以下是正在发生的事情的图像:
这是应用程序的布局:
这就是我想要的样子(Pixel 3):
但这是它在较小屏幕 (Nexus 5) 中的样子:
在 Xcode 中有一个名为 auto-resize 的功能可以自动为您调整内容的大小。 android工作室有类似的东西吗?
这是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">
<Button
android:id="@+id/resetButton"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginBottom="56dp"
android:background="#F70000"
android:text="Undo"
android:textColor="#000000"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="288dp"
android:layout_height="248dp"
android:background="#1750DF"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.685" />
</androidx.constraintlayout.widget.ConstraintLayout>
提前致谢!
更新
根据@tamir-abutbul 的回答,使用 layout_constraintHeight_percent 和 layout_constraintWidth_percent 使视图适合屏幕大小是一个很好的解决方法。 我已将顶部项目放置在网格布局中,您可以将其应用于您正在使用的任何顶部布局,主要要考虑的是此处的 layout_constraintHeight/Width_percent。 此外,对于图像,我已将背景设置为透明并将 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>
你的图像视图是这样的,因为你在其他视图中定义的宽度和高度很大,所以也许你可以在其他屏幕上尝试使用这个库来获得相同的尺寸
图像视图不会自动调整大小
因为您在 imageView
上使用固定尺寸值,所以您遇到了这个问题:
不同的 phone 有不同的屏幕尺寸,在您的布局中,您在视图上使用固定尺寸(例如,固定尺寸为 240dp
)结果是,在一个屏幕(您的 android 工作室预览屏幕)上可能看起来不错的内容在另一个屏幕(您的实际 phone)上可能看起来不太好。
如何修复
您可以使用这些属性使您的图像尺寸响应:
app:layout_constraintHeight_percent="0.25"
app:layout_constraintWidth_percent="0.25"
您需要在 android:layout_width
和 android:layout_height
0dp
大小
我所做的是根据屏幕尺寸告诉视图在宽度和高度上都等于 25%
,这将使您的视图响应所有屏幕尺寸,因此它将 "auto-resize"