使图像大小与屏幕大小保持一定的百分比

Sna*_*ake 4 android android-layout

在约束布局中,如果我希望图像水平居中并位于屏幕顶部,那么我可以这样做

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:id="@+id/imageView" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

如何使图像宽度和高度为屏幕宽度和高度的 25%?这是可行的吗?

谢谢

lel*_*man 15

更新:

使用新的ConstrainLayout 1.1.0,它已经实现了一种更好的方式来做到这一点!

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffff0000"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintWidth_percent=".25"
        app:layout_constraintWidth_default="percent"

        app:layout_constraintHeight_percent=".25"
        app:layout_constraintHeight_default="percent"
        />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

或者

您可以使用百分比放置指南,然后将 ImageView 约束到它们。有一种更简洁的方法来做这件事会很好,但我认为它仍然比带有权重的嵌套 LinearLayout 好得多

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/imageView"
        app:layout_constraintLeft_toLeftOf="@id/guideline_left"
        app:layout_constraintRight_toRightOf="@id/guideline_right"
        app:layout_constraintTop_toTopOf="@id/guideline_top"
        app:layout_constraintBottom_toBottomOf="@id/guideline_bottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffff0000"/>

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

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


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

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

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)