Orientation Center Orientation with Edge

in Android RelativeLayout we can set the textView exactly in the center of the screen using 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 go down a bit, I'm trying to add marginTop , but it seems that after using layout_centerVertical=true this becomes impossible. Any solution?

Expected result (slightly lower):

 top - - - - - - This is TextView (center vertical slight to bottom) - - bottom 
+5
source share
4 answers

Try using LinearLayout, which can easily fulfill 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> 
+6
source

Try the following:

Try using RelativeLayout, which can easily fulfill 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" andoird:marginTop="10dp" android:text="This is TextView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> 
+6
source

Why add a nested view inside the parent view ?

Use the 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 helps you.

+3
source

Today I encountered such a problem, and I figured it out. Sapce and LinearLayout are a good solution for 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> 
+1
source

Source: https://habr.com/ru/post/1207017/


All Articles