Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout - CenterInParent and marginTop

I has the following XML:

<RelativeLayout android:id="@+id/cover_box"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <ImageView android:id="@+id/cover"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <ImageView android:id="@+id/download" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/mark_download"
        android:layout_centerInParent="true" android:layout_marginTop="90px" />
</RelativeLayout>

But it's look's like the marginTop is being ignored.

like image 527
Marcos Vasconcelos Avatar asked Oct 25 '11 20:10

Marcos Vasconcelos


3 Answers

If you want the 2nd image to be 90dp under the center of the screen, you could replace it with a FrameLayout where you can control the padding to move your image downwards.

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"
    android:paddingTop="90dp">
    <ImageView android:id="@+id/download" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/mark_download"/>
</FrameLayout>
like image 57
ImR Avatar answered Oct 14 '22 16:10

ImR


When you use center in parent the view is put directly in the center. The margin top will only come into play if an object is within 90px of the top of your view. Thus pushing the centered view down to keep at least 90px of space on top of it. So it isn't being ignored but it is not having the effect that you think it should have.

like image 9
Bobbake4 Avatar answered Oct 14 '22 16:10

Bobbake4


I want the progressbar to shown under android:layout_centerInParent="true" so i have added a dummy TextView and set it to centerInParent .Then i put my progressbar under it. Now you can increase its distance from center in two ways . First by increasing marginTop in TextView and second increasing TextView height.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash" >

    <FrameLayout
        android:id="@+id/splash_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <com.s3.tdd.interfaces.CircularProgressBar
        android:id="@+id/circularprogressbar2"
        style="@style/Widget.ProgressBar.Holo.CircularProgressBar"
        android:layout_width="110dip"
        android:layout_height="110dip"
        android:layout_below="@+id/txt1"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
like image 3
Zar E Ahmer Avatar answered Oct 14 '22 16:10

Zar E Ahmer