约束布局-设置ImageView的宽度为整个屏幕大小的1/4

问题描述 投票:0回答:2

我如何仅使用XML将ImageView的布局设置为父布局大小的1/4。我也想避免使用嵌套在父ConstraintLayout内的LinearLayout来执行此操作。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


       <ImageView
        android:id="@+id/fragprofile_profile_picture"
        android:layout_width="0dp"
        android:layout_height="75dp"/>


</androidx.constraintlayout.widget.ConstraintLayout>
android android-constraintlayout
2个回答
0
投票

您可以使用Guideline

垂直准则。

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/start_guide_line"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25"
        />

水平准则

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/top_guide_line"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25"
        />

0
投票

您可以使用GuideLine来做到这一点,

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

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

    <ImageView
        android:id="@+id/fragprofile_profile_picture"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.