【问题标题】:Different size of ImageView on different devices不同设备上不同大小的 ImageView
【发布时间】:2021-04-18 06:30:06
【问题描述】:

我在具有不同 dpi 的不同屏幕上的 ImageView 有一点问题,如下所示:

在第二个屏幕上有图像视图的原始尺寸。但在第一个屏幕上,图像更大并移出。我如何为 ImageView 设置比例尺寸和边距。

这是我的代码:

<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>

提前致谢)

【问题讨论】:

  • 您是否使用像网格一样显示在列表中的图像我建议您使用 reyclerview 或 gride 视图来显示许多图像,并且网格视图还可以帮助您根据屏幕尺寸设置图像

标签: android xml imageview


【解决方案1】:

使用带垂直和水平参考线的约束布局代替RelativeLayout。这样,内容的大小就可以根据设备的屏幕大小进行调整。就像下面的示例一样。

<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>

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但我发现了这个:https://github.com/intuit/sdp 这是一个以sdp 表示尺寸的库。它只是带有 dp 的库,但针对不同的屏幕尺寸进行了缩放。

    【讨论】:

    • 这个库看起来很老了,你确定没有更现代的方法来解决这个问题吗?
    • 是的,但没有什么可改变的。您还可以通过创建自己的dimens 文件并为不同的屏幕尺寸指定通用值来实现此目的
    【解决方案3】:

    您可以对 ImageView 的宽度和高度使用百分比。例如,使用 ConstraintLayout 和 ImageView:

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

    【讨论】:

      【解决方案4】:

      有很多方法可以解决。上面的兄弟已经说过可以通过约束布局来解决。

      也可以通过线性布局来解决。只需使用 weight_sum。添加 2 查看添加顶部和底部,并在中间添加您的图像。重量完美地动态工作(你可能知道)。

      但如果我遇到这种情况,我会使用 sdpssp 库来解决它。该库大部分时间都运行良好。

      仅适用于字体/文本大小:ssp。 & 对于除字体以外的任何其他内容:sdp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-29
        • 2016-09-19
        • 1970-01-01
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多