Android relative layout center vertical with margin
Solution 1
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>
Solution 2
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>
Solution 3
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.
Solution 4
If you are not going to make a translate animation for the view, you can use the android:translationY
attribute to push it as much as you want (negative value for pushing up)
Solution 5
I encountered this kind of problem today and I figured out this way. Sapce
and LinearLayout
is a good solution to do this.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/base_background_color">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/base_toolbar_color"
android:minHeight="?attr/actionBarSize"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"/>
<!-- using LinearLayout to align ProgressBar to center vertical -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<ProgressBar
android:id="@+id/other_profile_progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"/>
</LinearLayout>
</RelativeLayout>
Comments
-
Abraham Putra Prakasa about 4 years
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 usinglayout_centerVertical=true
its become impossible. Any solution?Expected result (a little bit to bottom):
top - - - - - - This is TextView (center vertical slight to bottom) - - bottom