antoniom
antoniom

Reputation: 3241

layout_centerHorizontal is not centering

I have a relative layout, and inside it i place 3 items, 2 imageviews and one scrollview. My layout is like this...

<?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="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal"
    android:baselineAligned="true"
    android:background="@drawable/background">
    
    <RelativeLayout
        android:id="@+id/logosLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_margin="16dp"
        android:gravity="center">
                    
        <!-- An image will be placed here -->

    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_weight="1"
        android:background="#33ffffff"
        android:gravity="center"
        android:orientation="vertical">
        

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/up" />

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_margin="8dp"
            android:fadingEdgeLength="32dp"
            android:scrollbars="none" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageButton
                    android:id="@+id/hotelBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:background="@drawable/button_background"
                    android:contentDescription="@string/hotelBtnDesc"
                    android:gravity="center"
                    android:src="@drawable/icon1" />

                <TextView
                    android:id="@+id/hotelBtnDescTxtVw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:gravity="center"
                    android:text="@string/hotelBtnDesc"
                    android:textColor="#ffffff"
                    android:textSize="14sp" />

                    <!-- more scrollview items -->
            </LinearLayout>

        </ScrollView>
        
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" 
            android:src="@drawable/down" />
        
    </RelativeLayout>

</LinearLayout>

The above code produces the view shown here:

above code executed

You may note that the arrows are not aligned in the center, but are slightly moved to the right. Why is that, and how can I fix it ? Note that i have already used android:layout_centerHorizontal="true" to my imageviews.

Thank you in advance

Upvotes: 2

Views: 4277

Answers (5)

Akram
Akram

Reputation: 7526

Its problem with your

android:layout_margin="8dp"

remove it from both scrollview and imageView

and pass it to RelativeLayout direct.

or add

android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"

in your RelativeLayout.

and pass

android:layout_centerHorizontal="true"

to your scrollview

Upvotes: 2

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

It seems problem with your android:layout_margin="8dp" for your scrollview only and pass it to RelativeLayout direct but instead of using margin use padding = "8dp".

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

Try removing android:layout_weight="1" from your RelativeLayout and see if it works.


In your LinearLayout, the attribute android:orientation="horizontal" causing your display to be aligned horizontaly. Therefore your RelativeLayout is in center but since its shared with another layout thats why you cant note the difference. If you completely remove the first RelativeLayout (the one with id logosLayout) then i hope you'll see the second layout in center.

So, first you need to define the hierarchy of layout you require and then adjust your views accordingly.

Upvotes: 0

Sulabh Gajjar
Sulabh Gajjar

Reputation: 501

u need to set image view that is under the Relative layout set this image view width fill parent.

Upvotes: 0

thepoosh
thepoosh

Reputation: 12587

they are aligned to the center if you put the marginRight into consideration, try adding android:layout_margin="8dp" to the arrows.

Upvotes: 0

Related Questions