Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative Layout. Between 2 items

relative layout

How can I position an item between 2 other items and align it in the center? (please see the red button in the picture above) - How can I position it between the "Center Button" and "Bottom Button"?

Here is my relative layout code:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

    <Button 
        android:id="@+id/button_center" 
        android:text="Center" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_centerInParent="true"/>

    <!-- The new button should be between these 2 items -->

    <Button 
        android:id="@+id/button_bottom" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Bottom" 
        android:layout_centerHorizontal="true" 
        android:layout_alignParentBottom="true"/> 

    <Button 
        android:id="@+id/button_top" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Top" 
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true"/> 

    <Button 
        android:id="@+id/button_left" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Left" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true"/> 

    <Button 
        android:id="@+id/button_rignt" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Right" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true"/> 

    <Button 
        android:id="@+id/button_rel_right" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@id/button_right" 
        android:layout_alignTop="@id/button_rignt" 
        android:text="RelRight"/> 

    <Button 
        android:id="@+id/button_rel_left" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/button_left" 
        android:layout_alignTop="@id/button_left" 
        android:text="RelLeft"/> 

</RelativeLayout> 
like image 497
pleerock Avatar asked Oct 29 '11 16:10

pleerock


People also ask

What is relative layout with example?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

Which type of layout allows the components in relative position?

Android RelativeLayout enables you to specify how child views are positioned relative to each other. The position of each view can be specified as relative to sibling elements or relative to the parent.

What is the difference between relative layout and constraint layout?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

How do I create a layout in another layout?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.


1 Answers

You will need to add another layout so that the button will be exactly in the middle. For example, add this to your layout:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@+id/button_bottom"
    android:layout_alignLeft="@+id/button_center"
    android:layout_alignRight="@+id/button_center"
    android:layout_below="@id/button_center" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="new" />
</FrameLayout>
like image 149
Jan S. Avatar answered Oct 01 '22 11:10

Jan S.