The following XML code doesn't work to center an ImageView in a LinearLayout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/myimg" android:layout_width="36dip" android:layout_height="36dip" android:scaleType="fitXY" android:layout_gravity="center_horizontal" android:background="@drawable/my_background" /> </LinearLayout>
And this is RelativeLayout code which surprisingly works:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/myimg" android:layout_width="36dip" android:layout_height="36dip" android:scaleType="fitXY" android:layout_centerHorizontal="true" android:background="@drawable/my_background" /> </RelativeLayout>
Can someone tell me why? Thanks!
Android LinearLayout – Center Align To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.
Try layout_gravity and gravity attributes in the LinearLayout to make all the childs to be centered by default. This above is a splash screen activity. So there are two childs - ImageView and TextView. Both are center aligned.
CENTER_CROP. Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop" .
RelativeLayout attributes You can change layout_align parent like top, left, and right. android:layout_centerHorizontal : This attribute is used to center the element horizontally within its parent container. You can change layout_center like layout_centerVertical or layout_centerInParent .
You have to set vertical orientation on your LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/myimg" android:layout_width="36dip" android:layout_height="36dip" android:scaleType="fitXY" android:layout_gravity="center_horizontal" android:background="@drawable/my_background" />
Sorry, but above code didn't work for me, so i posting my Code which worked for me is
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" android:gravity="center_horizontal|center_vertical" android:orientation="vertical"> <ImageView android:id="@+id/ivPic" android:layout_width="70dp" android:layout_height="70dp" /> </LinearLayout>
I hope it helps for others who need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With