How to center two views in a relative layout?

The task is simple: above them are two buttons and a TextView . All widgets should be concentrated in a relative layout. My only idea is to create a third View widget and use it as the center axis for buttons. Any ideas? A backup layout is not a good solution.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/app_name" /> <View android:id="@+id/view_axis" android:layout_width="1dp" android:layout_height="1dp" android:layout_below="@id/tv_progress" android:layout_centerInParent="true" /> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_progress" android:layout_toLeftOf="@id/view_axis" android:text="@string/start" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_progress" android:layout_toRightOf="@id/view_axis" android:text="@string/stop" /> </RelativeLayout> 
+7
android alignment center relativelayout
source share
3 answers

If I understand correctly what you want, you can put the Button in the LinearLayout and the center, which

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/app_name" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/tv_progress"> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" /> </LinearLayout> 

I'm not sure what you meant by the “redundant layout”, but it is good if it gives you what you want.

+5
source share
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Spinner android:id="@+id/sp_rooms" android:layout_toLeftOf="@id/space" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Space android:id="@+id/space" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_registration" android:layout_centerVertical="true" android:layout_toRightOf="@id/space" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 
+2
source share

This will vertically and horizontally center the entire block consisting of textview + buttons

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_centerInParent="true"> <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" /> </LinearLayout> </LinearLayout> </RelativeLayout> 
0
source share

All Articles