Android约束布局: resize images for all resolutions

标签 android android-layout android-constraintlayout

我在一张背景图片上放置了小图片按钮。我已经使用约束布局管理了小图像的位置。但是现在,问题在于小图像按钮的大小。随着屏幕尺寸的变化,图像不会改变尺寸。

我尝试过使用 drawable-hdpi、drawable-xhdpi、drawable-xxhdpi,但可能是约束布局未拍摄该图像。如果我应用 layout_constraintDimensionRatio,图像会针对不同的屏幕更改其位置。或者我应该使用其他方法?

enter image description here

 <?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">
        <ImageView
            android:id="@+id/imgMainBg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg"
            android:layout_centerInParent="true"
            android:contentDescription="Background" />
        <ImageButton
            android:id="@+id/btnImg1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:contentDescription="btnImg1"
            android:scaleType="fitXY"
            android:src="@drawable/btnImg1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.585"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.183" />
        <ImageButton
            android:id="@+id/btnImg2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:contentDescription="btnImg2"
            android:scaleType="centerCrop"
            android:src="@drawable/btnImg2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.944"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.352" />
    </android.support.constraint.ConstraintLayout>

最佳答案

ConstraintLayout 的问题在于它使用一些准则、屏幕的一部分或其他同级 View 来约束 View 。随着屏幕尺寸的增大或缩小,约束保持不变。因此,ConstraintLayout 上 View 的相对定位确实会根据屏幕尺寸发生变化。如果您在 drawable 文件夹中使用 png 或其他图像,则可以在 drawable-hdpidrawable-ldpi 和其他文件夹中提供不同的图像。图片应具有不同的大小,以便您能够看到图片在不同的 dpi 屏幕上测试时变大和变小。

关于Android约束布局: resize images for all resolutions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51230298/

相关文章:

android map 项目未清除

android - 我怎样才能下载android android sdk r12

c# - Xamarin 空 CarouselView

android - 将ImageView与布局android的右侧对齐

android - ConstraintLayout 最后一个 View 被裁剪

android - 如何将 GIF 转换为 Mp4?

android - CollapsingToolBarLayout 和 NestedScrollView 平滑滚动问题

java - 当我在布局之间滑动时如何打开音频文件?

android - 以编程方式添加时左上角的 ConstraintLayout View

android - 从属性检查器中指定时,约束布局会忽略边距属性