2

I would like to align this ImageView in center (horizontal and vertical):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header"
    android:gravity="center">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_event_black_18dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@color/sliding_tab_border_color"/>

</RelativeLayout>

If I don't add the line (the View) after my ImageView it's perfect, it's work, but with this line the gravity doesn't work.

Thanks by advance!

N J
  • 27,217
  • 13
  • 76
  • 96
anthony
  • 7,653
  • 8
  • 49
  • 101

5 Answers5

2

You need to change some property and your view is perfect. Just check below and you see I change gravity property in .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header"
    >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@color/sliding_tab_border_color"/>

</RelativeLayout>

You need to use

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

instead of

android:layout_gravity="center_horizontal|center_vertical"

because This is works in LinearLayout. But, for RelativeLayout you need to use above property for gravity set.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
1

Try android:layout_centerInParent="true" property of Relativelayout and remove the gravity.

<ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_event_black_18dp"/>
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
0

You shoud use layout_alignBottom instead layout_alignParentBottom in View element.

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_alignBottom="@+id/icon"
    android:background="@color/sliding_tab_border_color" />
Peter-Yu
  • 191
  • 7
0

Change your code with this . It is working and i changed the icon to the one which was available with me, you can use your icon

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header">



    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/li">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"/>
    </LinearLayout>

</RelativeLayout>
ZaidRehman
  • 1,631
  • 2
  • 19
  • 30
0

You can use one more layout element to merge ImageView and View elements. Like this;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header"
    android:gravity="center">

    <RelativeLayout
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="@dimen/slidingtab_item_icon_size"
            android:layout_height="@dimen/slidingtab_item_icon_size"
            android:src="@drawable/ic_event_black_18dp"/>

        <View
            android:layout_below="@+id/icon"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/sliding_tab_border_color"/>

    </RelativeLayout>

</RelativeLayout>
MSalihKarakasli
  • 422
  • 3
  • 13