android - 不同设备上不同大小的 ImageView

标签 android xml imageview

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

screen from emulator

screen from real device

在第二个屏幕上有 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>

提前致谢)

最佳答案

使用带垂直和水平引用线的约束布局代替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>

关于android - 不同设备上不同大小的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65698627/

相关文章:

android - 从 CustomListViewAdapter 中的 Imageview 获取内容以通过 Facebook 共享

Android - 自定义标题栏渐变问题

android - 为什么 Google 不提供 float 操作按钮小部件?

xml - HTML5 中的 SVG – 什么时候需要 XML 声明 `<?xml version="1. 0"encoding="UTF- 8"?>`?

android - 如何制作一个 GridLayout,其中元素平均填充网格?

android - 将图像添加到android中的表面 View

android - 在android中使用多方和json将图像上传到服务器

java - 如何在android/java中合成钢琴音色

java - Xml转JSon,字符问题

java - 使用 JavaFX 的骰子模拟器,图像未出现