Relative layout aligns an element below and to the middle of another element

I am using a relative layout and want to align the TextView below and in the middle of the button, as shown in the attached image. I can get it from below using BELOW, but I can’t determine how to align their horizontal centers.

enter image description here

+4
source share
2 answers

The easiest way is to put the image and the text image in a relative layout.

<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_centerHorizontal="true" android:text="TextView" /> </RelativeLayout> 

Edit

To do this in a single layout, add android:drawableTop to your TextView

 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:drawableTop="@drawable/ic_launcher"/> 
+6
source

A simple way to do this:

  • Set the layout_width attribute to match_parent, so the width
  • Then set the "gravity" attribute to "center_horizontal"

Example:

 <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" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:layout_below="@+id/imageView1" android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:gravity="center_horizontal" /> </RelativeLayout> 
0
source

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


All Articles