【问题标题】:Android constraint layout: resize images for all resolutionsAndroid约束布局:为所有分辨率调整图像大小
【发布时间】:2018-07-08 22:42:26
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    ConstraintLayout 的问题在于它使用一些指导线、屏幕的一部分或其他同级视图来限制视图。随着屏幕尺寸的增大或缩小,约束保持不变。因此,ConstraintLayout 上视图的相对位置会根据屏幕尺寸而变化。如果您在 drawable 文件夹中使用 png 或其他图像,您可以在 drawable-hdpidrawable-ldpi 和其他文件夹中提供不同的图像。图像应该有不同的大小,以便您能够看到图像在不同的dpi 屏幕上测试应用时放大和缩小。

    【讨论】:

    • 感谢您的回复。是的,我正在使用所有 png 图像。如果我将图像放在 drawable-hdpi 、 drawable-ldpi 和其他文件夹中,那么建议删除 app:layout_constraintBottom_toBottomOf="parent"、app:layout_constraintLeft_toLeftOf="parent" 、 app:layout_constraintRight_toRightOf="parent" 、 app:layout_constraintTop_toTopOf="家长”。如果我删除这些,图像不能坚持不同的屏幕尺寸的原始位置。我还应该将高度和宽度保持为 wrap_content 吗?
    • 我认为您根本不需要删除这些约束。你可以拥有所有你想要的约束。我相信您正在尝试调整大小,您应该对heightwidth 使用wrap_content
    • 此外,如果您使用约束集和heightwidth,则图像可能会在某些屏幕上的某些部分被截断,因为高 dpi 屏幕并不一定意味着它是一款大屏手机。在这种情况下,您必须为available-widthsmallest-width 限定符提供单独的layout 文件夹。更多关于这些资源here
    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2018-02-09
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多