Android Relative Layout Center Vertical With Margin
Answer :
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>
Try to use LinearLayout which can easily done your requirement using weight :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical"> <TextView android:text="This is TextView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
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.
Comments
Post a Comment