2
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="left" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="center" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="right" />
    </LinearLayout>

I know I can also use Relativelayout and set alignparentRight=true,to let the right button on the right of this parent,But I just want to know,if I use a LinearLayout,how can I make the right button on the right of it's parent? any help thanks

lightman1988
  • 141
  • 1
  • 13

5 Answers5

7

If you have only three or two child you can use FrameLayout instead of LinearLayout. But if you want to use LinearLayout do like this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="left" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="center" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="right" />

</LinearLayout>

Using a FrameLayout as parent

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:text="left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:text="right" />

</FrameLayout>

Why layout_gravity="right" is not working for Horizontal LinearLayout explained here

For a horizontal Linear Layout the following values make sense:

  • top
  • center
  • bottom

That is because the children of a horizontal Linear Layout are layed out horizontally one after the other. Only thing can be controlled using the android:layout_gravity is how a child view is positioned vertically.

For a vertical Linear Layout the following values make sense:

  • left
  • center
  • right

That is because the children of a vertical Linear Layout are layed out vertically one below the other. Only thing can be controlled using the android:layout_gravity is how a child view is positioned horizontally.

Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • thanks,and If I use LinearLayout like you say,without the Layout_gravity property it also work because the layout_weight.but thank you – lightman1988 Oct 27 '14 at 10:08
  • in `LinearLayout` doesn't need `layout_gravity` only `layout_weight` is required – Kaushik Oct 27 '14 at 10:12
  • yeah,but my question is ,if I have two button in a horizontal LinearLayout(android:layout_width="match_parent") ,one's layout_gravity="left",another is layout_gravity="right",why the right button can't on the right of it's parent. I was very very confused this. – lightman1988 Oct 27 '14 at 10:16
  • 1
    for ur requirement use `Framelayout` why `layout_gravity` is not working [explained here.](http://sandipchitale.blogspot.in/2010/05/linearlayout-gravity-and-layoutgravity.html) Please read that. – Kaushik Oct 27 '14 at 10:30
0

Put the center button in a RelativeLayout with weight 1 so that the center layout takes the whole area.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />
    <RelativeLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="center" />
    </RelativeLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>

For two buttons

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />
    <RelativeLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right" />
    </RelativeLayout>
</LinearLayout>
Naveen
  • 1,703
  • 13
  • 22
  • thank you,it works. but If I have only two child in a LinearLayout ,how to make them,one on the left of the parent,another on the right of the parent. how can I set the layout_gravity ? – lightman1988 Oct 27 '14 at 09:04
  • and If I set the RelativeLayout gone ,it just not work – lightman1988 Oct 27 '14 at 09:11
  • For 2 buttons you can remove the third button and put the android:gravity="right" on the RelativeLayout – Naveen Oct 27 '14 at 09:31
0

Why not use weights,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="left"
        android:layout_weight="33"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="left" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="34"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="33"
        android:gravity="right"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right" />
    </LinearLayout>
</LinearLayout>

And if you have two buttons

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="left"
        android:layout_weight="50"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="left" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:gravity="right"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right" />
    </LinearLayout>
</LinearLayout>

Note: Implementing this way involves many layouts to be added and will cause problems when the app is pretty heavy. Its always recommended to use the Relative Layout.

codePG
  • 1,754
  • 1
  • 12
  • 19
  • thanks,but I was also confused how to use the layout_gravity? why I set layout_gravity="right" on the right button ,but it not works. – lightman1988 Oct 27 '14 at 09:22
  • 1
    Bad Idea. We shouldn't use too many layouts if single can do the job. Number of layouts affect application performance and even cause crashes if depth is too much. Use relative layout instead. Any specific requirement to use linear layout?? – Rohit5k2 Oct 27 '14 at 09:31
  • @Rohit5k2 Thanks for pointing that out. I will add a note on my answer. But the answer was so to show the OP the workaround. The OP already knew to implement using the Relative Layout. – codePG Oct 27 '14 at 09:34
  • Yeah, that's why I asked @lightman1988 if he/she has any specific thing in mind which forces him/her to use linear layout. :) – Rohit5k2 Oct 27 '14 at 09:37
  • Thanks,I just want know why layout_weight="right" didn't work.if I want right button on the right of button.If I have one child,It works,but why two or more can't – lightman1988 Oct 27 '14 at 09:43
  • @lightman1988 That is the nature of a **horizontal** Linear Layout. To learn more have a quick look at these tutorials. http://www.youtube.com/watch?v=DxfYeAUd238&index=47&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR – codePG Oct 27 '14 at 09:47
  • thank you very much,but what is the nature of a horizontal LinearLayout,what do you mean – lightman1988 Oct 27 '14 at 09:54
  • `layout_gravity` has certain limitations for both the orientations of the Linear Layout. Kindly check the link I posted in my earlier comment. You have a clear explanation there. – codePG Oct 27 '14 at 09:57
0

Use

 Relative layout instead of 
 linear layout. 

and use layout_alignParentRight to make it right of the parent.

 <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />
</RelativeLayout>
justDroid
  • 343
  • 2
  • 9
  • It seems he already knew that method. He just wanted to try the same with Linear Layout. Kindly read the question fully! – codePG Oct 27 '14 at 09:16
  • thanks,I know this. But I just want to know,if I use a LinearLayout,how can I make the right button on the right of it's parent? – lightman1988 Oct 27 '14 at 09:16
0

Another way to do this if you don't want potential overlapping cause by FrameLayout or RelativeLayout and still wanna use wrap_content width which you can't if you align views properly in RelativeLayout to avoid overlapping:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />

</LinearLayout> 

This method is pretty common and is used by (at least last time i checked it was) AlertDialog in the appcompat lib

Sourabh
  • 8,243
  • 10
  • 52
  • 98