Search code examples
androidandroid-layoutimageviewandroid-drawable

Editing ImageView layout to surround drawable


I have a layout to show a dashed line drawable that has a width of 1 do. Currently the layout hight and width are stretching the page. But I only want it to surround the drawable image what needs to be changed to do this? Current layout

dashes.xml

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dashedVuew"
    android:src="@drawable/dashedLine"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    android:layout_gravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

Solution

  • So you want a layout with only one ImageView? No other Views?

    If so, then there is no need to use ConstraintLayout at all! Just use FrameLayout with one ImageView along with layout_gravity="center".

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/dashedVuew"
            android:src="@drawable/dashedLine"
            android:layout_width="match_parent" // or wrap_content
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>
    
    </FrameLayout>
    

    P.S. There is no need to define xmlns in your ImageView!