Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android relative layout center vertical with margin

in Android's RelativeLayout we can set textView exact in the center of the screen with this code:

<TextView
    android:text="This is TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

result:

top
-
-
-
-
This is TextView (center vertical)
-
-
-
-
bottom

But I need the textView to be a little bit to the bottom, I try add marginTop but seems like after using layout_centerVertical=true its become impossible. Any solution?

Expected result (a little bit to bottom):

top
-
-
-
-
-
-
This is TextView (center vertical slight to bottom)
-
-
bottom
like image 543
Abraham Putra Prakasa Avatar asked Nov 17 '14 09:11

Abraham Putra Prakasa


Video Answer


2 Answers

Try this:

Try to use RelativeLayout which can easily done your requirement using weight :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">
      <TextView
          android:id="@id/dummy"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true" />
      <TextView
          android:layout_below="@id/dummy"
          android:layout_marginTop="10dp"
          android:text="This is TextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
</RelativeLayout>
like image 144
No Name Avatar answered Sep 18 '22 13:09

No Name


Why should add nested view inside parent view ?

Use paddingTop attribute.

Example:

<RelativeLayout
    android:id="@+id/rlParentView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:paddingTop="10dp"/>

</RelativeLayout>

Hope this will help you.

like image 44
Hiren Patel Avatar answered Sep 19 '22 13:09

Hiren Patel