简体   繁体   English

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

[英]How to auto-resize imageview for different screen sizes android studio?

I am starting to develop apps for android devices using Android Studio.我开始使用 Android Studio 为 android 设备开发应用程序。 While I was testing my app for screen size compatibilty I noticed that the imageviews do not auto-resize.当我测试我的应用程序的屏幕尺寸兼容性时,我注意到图像视图不会自动调整大小。

The imageview placement looks completely different in different screen sizes. imageview 的位置在不同的屏幕尺寸下看起来完全不同。 If the screen is small enough, the imageview will sometimes be placed outside of the viewscope.如果屏幕足够小,imageview 有时会被放置在视野范围之外。

I have looked all over the internet but I am a little confused on how to make the app compatible with most screen sizes (not including tablets).我浏览了整个互联网,但对于如何使应用程序与大多数屏幕尺寸(不包括平板电脑)兼容,我有点困惑。 I went to the android website and they said to use wrap_content.我去了 android 网站,他们说要使用 wrap_content。 but if I use this, I wont be able to adjust the height and width of the imageview to how I want it.但如果我使用它,我将无法将 imageview 的高度和宽度调整为我想要的。

Does anyone know how to make imageview auto-resize accordingly to the screen size?有谁知道如何使 imageview 根据屏幕尺寸自动调整大小?

Here are images of what is happening:以下是正在发生的事情的图片:

This is the layout of the app:这是应用程序的布局:

这是布局

This is how I want it to look like (Pixel 3):这就是我希望它的样子(Pixel 3):

在此处输入图像描述

But this is how it looks like in a smaller screen(Nexus 5):但这就是它在小屏幕(Nexus 5)中的样子:

在此处输入图像描述

In Xcode there is a feature called auto-resize that automatically resizes the content for you.在 Xcode 中有一个称为自动调整大小的功能,可以自动为您调整内容的大小。 Does android studio have something similar to that? android studio有类似的吗?

This is the xml:这是 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>

Thank you in advance!先感谢您!

the imageviews do not auto-resize图像视图不会自动调整大小

Because you are using a fixed size value on your imageView you are having this problem:因为您在imageView上使用固定大小值,所以您遇到了这个问题:

Different phones got different screen size , in your layout you are using fixed size on your view (fixed size is 240dp for example) and the result is that what may look good on one screen (your android studio preview screen) will not look good on another screen (your actual phone).不同的手机有不同的屏幕尺寸,在您的布局中,您在视图上使用固定尺寸(例如固定尺寸为240dp ),结果是在一个屏幕上看起来不错(您的 android 工作室预览屏幕)看起来不太好另一个屏幕(您的实际手机)。


How to fix怎么修

You can use those attributes to make your image responsive in size:您可以使用这些属性使您的图像在尺寸上响应:

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

You will need to give your image 0dp size both in android:layout_width and android:layout_height您需要在android:layout_widthandroid:layout_height中为您的图像提供0dp大小

What I did was to tell the view to be equal to 25% both in width and height according to the screen size, this will make you view responsive to all screen size and thus it will "auto-resize"我所做的是根据屏幕尺寸告诉视图的宽度和高度都等于25% ,这将使您的视图响应所有屏幕尺寸,因此它将“自动调整大小”

your imageview like that because your define width and height large in other view, so maybe you can try this library for same size in other screen你的 imageview 像这样,因为你在其他视图中定义的宽度和高度很大,所以也许你可以在其他屏幕上尝试这个库的大小相同

https://github.com/intuit/sdp https://github.com/intuit/sdp

Update更新

Following the answer by @tamir-abutbul, it is a nice workaround to use layout_constraintHeight_percent and layout_constraintWidth_percent to make view fit according to screen size.根据@tamir-abutbul 的回答,使用 layout_constraintHeight_percent 和 layout_constraintWidth_percent 使视图适合屏幕尺寸是一个很好的解决方法。 I have placed the top items in a Grid Layout, you can apply this to any of your top layout which you are using, the main thing to consider is the layout_constraintHeight/Width_percent here.我已将顶部项目放置在网格布局中,您可以将其应用于您正在使用的任何顶部布局,要考虑的主要事项是此处的 layout_constraintHeight/Width_percent。 Also, for the image, I have set the background transparent and changed scaleType to "fitCenter" so that the image keeps its aspect ratio on any screen.此外,对于图像,我将背景设置为透明并将 scaleType 更改为“fitCenter”,以便图像在任何屏幕上保持其纵横比。 hope this helps.希望这可以帮助。 You can change android to androidx.您可以将 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM