【问题标题】:How to auto-resize imageview for different screen sizes android studio?如何为不同的屏幕尺寸android studio自动调整imageview的大小?
【发布时间】:2020-02-09 09:47:21
【问题描述】:

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

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

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

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

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

这是应用的布局:

这就是我想要的样子(像素 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>

提前谢谢你!

【问题讨论】:

    标签: java android android-layout android-developer-api


    【解决方案1】:

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

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

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


    如何解决

    您可以使用这些属性使您的图片在尺寸上具有响应性:

    app:layout_constraintHeight_percent="0.25"
    app:layout_constraintWidth_percent="0.25"
    

    您需要在android:layout_widthandroid:layout_height 中为您的图片提供0dp 大小

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

    【讨论】:

    • 这对我有用。请注意,这个尺寸百分比不应该基于父视图而不是屏幕尺寸视图吗?
    • @Andrew Hey,答案有点晚了,但来自docslayout_constraintWidth_percentlayout_constraintHeight_percent :将设置这个的大小尺寸占父级的百分比
    【解决方案2】:

    更新

    根据@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> 
    

    【讨论】:

    • 我相信我已经尝试过了,但我仍然把它放在错误的位置。当屏幕尺寸改变时,这会改变图像视图的尺寸吗?还是图像视图会保持相同的大小?
    • 你能分享你的布局xml吗,因为有创建布局的技术。让我们看看你在用什么。
    • 您好!我在问题中添加了 XML 代码。你能告诉我我做错了什么@Adnan
    【解决方案3】:

    你的 imageview 是这样的,因为你在其他视图中定义的宽度和高度很大,所以也许你可以在其他屏幕上尝试这个库以获得相同的尺寸

    https://github.com/intuit/sdp

    【讨论】:

    • 谢谢!这也是一个很好的选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2021-05-07
    相关资源
    最近更新 更多