Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_gravity not working in horizontal linear layout

I have been trying to use layout_gravity in a horizontal linear layout. But it doesn't seem to work. I don't want to use a relative layout or any other layout. I want to know why it is not working with linear layout.I have earlier used layout_gravity in vertical linear layouts and it has worked in the way I expected.

<LinearLayout     android:id="@+id/shata"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     >     <TextView         android:id="@+id/title_textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:textColor="@color/black"         android:text="shata1"         android:textAppearance="?android:attr/textAppearanceMedium"         />     <TextView         android:id="@+id/map_imageview"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_gravity="right"         android:textAppearance="?android:attr/textAppearanceMedium"         android:text="shata2"/>     </LinearLayout> 

As you can see the the linear layout has a orientation=horizontal and width=match_parent(it is just inside the root layout). But both the TextViews are displayed sticking to the left of the LinearLayouteven though I have given their layout_gravity=center and layout_gravity=right respectively.

like image 452
Ashwin Avatar asked Oct 12 '15 08:10

Ashwin


People also ask

What is the default orientation of a Linearlayout?

The default orientation is horizontal.

What is true about Layout_gravity attribute in Linearlayout?

The difference between the gravity and layout_gravity XML attributes in Android is that the android:gravity attribute is used to arrange the position of the content inside a View (for example text inside a Button widget) while the android:layout_gravity is used to arrange the position of the entire View relative to ...

How do you change linear layout to vertical?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

What is the different value of the orientation attribute in Linearlayout?

The orientation attribute is used to arrange its children either in horizontal or vertical order. The valid values for this attribute are horizontal and vertical.

Why does my linearlayout ignore the gravity of a view?

For some reason, if one of the elements in the LinearLayout (Horizontal) has a different height than the rest of the views in the layout and is set to have the same gravity as the others, the gravity is effectively "ignored".

What is layout_gravity=bottom in a linearlayout?

layout_gravity in a LinearLayout can only change the objects position perpendicular to the orientation. If orientation is horizontal, you can specify top and bottom, not left and right. – Raghu Teja Oct 12 '15 at 8:57 @chitti : But I have seen layout_gravity=bottom work in veritcal linear layouts. – Ashwin Oct 12 '15 at 9:09

What is the difference between layout_gravity and gravity in Android?

. Gravity is used to position the content of View (Textview) and layout_gravity is used to position the TextView based on its parent (LinearLayout) . . We will have the same output, Text1 and Text2 will not be centered vertically since the gravity is applied to views that have a height defined as “wrap_content”.

Does layout_gravity work with horizontal orientation?

layout_gravity doesn't work with Horizontal orientation in LinearLayout 0 button alignment left start and right end android kotlin 0 Layout_gravity start & end doesn't spread the elements to two side, but packed close Related 663 How do I align views at the bottom of the screen? 1419


1 Answers

Short answer: If you have a horizontal LinearLayout, then each child can only have layout_gravity values of top, bottom, and center.

The reason for this is because you've already told the LinearLayout to align each child horizontally, left-to-right in the order you specify, which means that it only lets you specify the vertical alignment of each child in the layout_gravity parameter, and the opposite applies when your LinearLayout is vertical.

There's a good link explaining how the various gravities work here.

like image 70
Matt Taylor Avatar answered Sep 20 '22 04:09

Matt Taylor