如何为不同屏幕尺寸的android studio自动调整imageview的大小?

Ale*_*lin 3 java android android-layout android-developer-api

我开始使用 Android Studio 为 android 设备开发应用程序。当我测试我的应用程序的屏幕尺寸兼容性时,我注意到图像视图不会自动调整大小。

imageview 的位置在不同的屏幕尺寸下看起来完全不同。如果屏幕足够小,imageview 有时会被放置在 viewscope 之外。

我已经浏览了整个互联网,但我对如何使该应用程序与大多数屏幕尺寸(不包括平板电脑)兼容感到有些困惑。我去了android网站,他们说要使用wrap_content。但是如果我使用它,我将无法将图像视图的高度和宽度调整为我想要的。

有谁知道如何根据屏幕尺寸使 imageview 自动调整大小?

以下是正在发生的事情的图像:

这是应用程序的布局:

这是布局

这就是我想要的样子(Pixel 3):

在此处输入图片说明

但这就是它在较小屏幕(Nexus 5)中的样子:

在此处输入图片说明

在 Xcode 中有一个称为自动调整大小的功能,可以自动为您调整内容大小。android studio 有类似的东西吗?

这是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>
Run Code Online (Sandbox Code Playgroud)

先感谢您!

Tam*_*bul 9

图像视图不会自动调整大小

因为您使用的是固定大小值,所以您imageView遇到了这个问题:

不同的手机有不同的屏幕尺寸,在您的布局中,您在视图上使用固定尺寸(240dp例如固定尺寸),结果是在一个屏幕(您的 android studio 预览屏幕)上看起来不错的在另一个屏幕上看起来不好看屏幕(您的实际手机)。


怎么修

您可以使用这些属性使您的图像在大小上具有响应性:

app:layout_constraintHeight_percent="0.25"
app:layout_constraintWidth_percent="0.25"
Run Code Online (Sandbox Code Playgroud)

您需要0dp在 inandroid:layout_widthandroid:layout_height

我所做的是25%根据屏幕尺寸告诉视图的宽度和高度相等,这将使您查看响应所有屏幕尺寸,因此它会“自动调整大小”


Adn*_*mur 5

更新

根据@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> 
Run Code Online (Sandbox Code Playgroud)