Home > Net >  i am trying to center this image in the linear layout but its not working, what can i do
i am trying to center this image in the linear layout but its not working, what can i do

Time:10-18

I am trying to center the image in this linear layout, but it is only aligning to the left what can i do to fix this.

 <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
    
       <ImageView
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:src="@android:drawable/ic_menu_edit" />
 </LinearLayout>

CodePudding user response:

Try to set android:layout_gravity="center"

<ImageView
         android:layout_gravity="center"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:src="@android:drawable/ic_menu_edit" />

CodePudding user response:

Add android:gravity="center" to LinearLayout

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:gravity="center">
    
       <ImageView
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:src="@android:drawable/ic_menu_edit" />
 </LinearLayout>

Or Add android:layout_gravity="center" to the ImageView

<ImageView
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="center"
         android:src="@android:drawable/ic_menu_edit"
         />

It should work either way.

  • Related