WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

Constraint Layout - Set width of ImageView 1/4th the size of entire screen

How would I set the layout of an ImageView to be 1/4th the size of the parent layout using only XML. I also want to avoid using a LinearLayout that is nested inside the parent ConstraintLayout to do this.

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

Upvotes: 0

Views: 616

Answers (2)

sadat
sadat

Reputation: 4342

You can use GuideLine to do that,

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

Upvotes: 1

Hello World
Hello World

Reputation: 740

You can use Guideline.

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

Horizontal Guideline

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

Upvotes: 1

Related Questions