简体   繁体   English

ImageView 在不同设备上的大小不同

[英]Different size of ImageView on different devices

I have a little problem with ImageView on differents screens with different dpi like this:我在具有不同 dpi 的不同屏幕上使用 ImageView 有点问题,如下所示:

来自模拟器的屏幕

真实设备的屏幕

On second screen there are original dimensions of imageviews.在第二个屏幕上有图像视图的原始尺寸。 But on first screen images are bigger and moved out.但是在第一个屏幕上,图像更大并且移出了。 How i can set proportional dimensions and margins to ImageView.我如何将比例尺寸和边距设置为 ImageView。

Here is my code:这是我的代码:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/textLayoutIntro">

        <ImageView
            android:id="@+id/first_slide_second_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@mipmap/first_slide_second_image"
            android:layout_alignParentStart="true"
            android:layout_alignStart="@+id/firstSlideTelephone"
            android:layout_marginTop="100dp"
            android:layout_marginStart="20dp"/>

        <ImageView
            android:id="@+id/first_slide_third_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/first_slide_third_image"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:layout_alignParentEnd="true"
            android:layout_alignEnd="@+id/firstSlideTelephone"
            android:layout_marginEnd="30dp"
            android:layout_marginTop="100dp"/>


        <ImageView
            android:id="@+id/firstSlideTelephone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/first_slide_telephone"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"/>


        <ImageView
            android:id="@+id/first_slide_first_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@mipmap/first_slide_first_image"
            android:layout_marginStart="-30dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:maxWidth="138dp"
            android:maxHeight="138dp"/>


        <ImageView
            android:id="@+id/first_slide_fourth_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@mipmap/first_slide_fourth_image"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="100dp"/>


    </RelativeLayout>

Thanks in advance)提前致谢)

Instead of RelativeLayout, use constraintlayout with vertical and horizontal guidelines.代替 RelativeLayout,使用带有垂直和水平指南的 constraintlayout。 That way, the content sizes up or down to whatever device screen size.这样,内容的大小就可以根据设备的屏幕大小来调整。 Like the sample below.就像下面的示例。

<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:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:clickable="true"
    tools:context="com.example.example.Fragments.ExampleFragment">


    <android.support.constraint.Guideline
        android:id="@+id/fragment_guideline_v_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.1" />

    <android.support.constraint.Guideline
        android:id="@+id/fragment_guideline_v_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.9" />


    <android.support.constraint.Guideline
        android:id="@+id/fragment_guideline_h_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <android.support.constraint.Guideline
        android:id="@+id/fragment_guideline_h_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9" />

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:contentDescription="@string/none"
            app:layout_constraintStart_toEndOf="@+id/fragment_guideline_h_1"
            app:layout_constraintEnd_toStartOf="@+id/fragment_guideline_h_2"
            app:layout_constraintTop_toBottomOf="@+id/fragment_guideline_v_1"
            app:layout_constraintBottom_toTopOf="@+id/fragment_guideline_v_2"
            app:srcCompat="@drawable/forgot_password_arrow_back" />

</android.support.constraint.ConstraintLayout>

I've had same problem but I've found this: https://github.com/intuit/sdp It's a library with dimensions represented by sdp .我遇到了同样的问题,但我发现了这个: https://github.com/intuit/sdp这是一个维度由sdp表示的库。 It just library with dp but scaled for different screen sizes.它只是带有dp的库,但针对不同的屏幕尺寸进行了缩放。

You can use percentage for ImageView width and height.您可以使用 ImageView 宽度和高度的百分比。 For example, use ConstraintLayout and for ImageView:例如,使用 ConstraintLayout 和 ImageView:

<ImageView
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_constraintWidth_percent="0.7" />

It's possible to solve many ways.可以通过多种方式解决。 The above bros already said possible to solve by Constraint layout.上面的兄弟已经说了可以通过Constraint layout来解决。

It's also possible to solve by Linear layout.也可以通过线性布局来解决。 Just use weight_sum.只需使用 weight_sum。 Add 2 View add the top and bottom and at the middle add your image.添加 2 视图添加顶部和底部,并在中间添加您的图像。 Weight perfectly work dynamically (may you know).重量完美地动态工作(你知道吗)。

But if I was in this situation, I would be solved it using sdp & ssp library.但是如果我遇到这种情况,我会使用sdp & ssp库来解决它。 The library most of the time works perfectly.图书馆大部分时间都运作良好。

only for font/text size: ssp .仅适用于字体/文字大小: ssp & For any others except font: sdp & 对于除字体以外的任何其他字体: sdp

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

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