Setting the center of gravity of changing the view of a child’s view in another view of the child in a horizontal layout?

In my horizontal LinearLayout, I set gravity in one view as center_vertical, and then I tried to set layout_gravity for the second view, but this view is aligned with centered text in the first view!

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="100dp" android:layout_height="200dp" android:layout_gravity="top" android:background="@drawable/border" android:gravity="center_vertical" android:text="layout_gravity=top gravity=center_vertical" > </TextView> <TextView android:layout_width="100dp" android:layout_height="200dp" android:layout_gravity="top" android:background="@drawable/border" android:gravity="top" android:text="layout_gravity=top gravity=top" > </TextView> </LinearLayout> 

enter image description here

And here is the same code, but for a vertical layout. Note that the desired behavior works correctly in a vertical layout.

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="200dp" android:layout_height="100dp" android:layout_gravity="left" android:background="@drawable/border" android:gravity="center_horizontal" android:text="layout_gravity=left gravity=center_horizontal" > </TextView> <TextView android:layout_width="200dp" android:layout_height="100dp" android:layout_gravity="right" android:background="@drawable/border" android:gravity="right" android:text="layout_gravity=right gravity=right" > </TextView> </LinearLayout> 

enter image description here

I can simply use RelativeLayout or perhaps another nested LinearLayout to fix this problem. But I ask this question because I would like to know if I do not understand how gravity and layout_gravity work! It is important for me that I understand how these basic attributes work. Thanks

+5
source share
3 answers

If you want one centered and one top, you need to add a baseline alignment flag in LinearLayout

 android:baselineAligned="false" 

By default, the layout aligns the baseline for children. This can lead to such problems when children use different values ​​of gravity.

The difference with the vertical is that the original lines cannot be horizontally aligned.

Cm:

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:baselineAligned

Additional Information. This seems to explain the problem better than I can:

http://www.doubleencore.com/2013/10/shifty-baseline-alignment/

+7
source

gravity is how the view itself positions its contents within an area defined by its width and height. layout_gravity is how the parent look positions the child in the parent area. You can usually just set gravity to LinearLayout instead of setting layout_gravity for all of your children.

Note that for LinearLayouts gravity affects only the axis, which is not an orientation, for example. the top and bottom will refer to the horizontal LinearLayout, but the left and right (or the beginning and end) will not matter.

0
source

if you are trying something not to "connect to each other", that is, you need some space between the two widgets, then use an empty widget and use the scales

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="100" > <TextView android:layout_width="100dp" android:layout_height="200dp" android:layout_weight="40" android:gravity="center_vertical" android:text="layout_gravity=top gravity=center_vertical" > </TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" /> <TextView android:layout_width="100dp" android:layout_height="200dp" android:layout_weight="40" android:gravity="right" android:text="layout_gravity=top gravity=top" > </TextView> </LinearLayout> 

Main Answer Image

You did not know what you wanted, this is what I could interpret as your desired conclusion. But I will explain to you how it works. For more information on weights, you can go to http://developer.android.com/reference/android/widget/package-summary.html

There is something called “widget” and “widget content”. Speaking of linearlayout with layout_gravity, you set its gravity as a “widget” in the main view, and using gravity you set gravity on the content in this linear mode

It is symbolic with the textview expression, if you use layout_gravity (for example, equal to the center), then the text image will be centered in its space (that is, it will be centered in width if the orientation of the layout is vertical, and it will be centered in height if the layout_orientation is horizontal )

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:text="Second Activity" /> </LinearLayout> 

Horizontal linear_layout

See the same in vertical orientation.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:text="Second Activity" /> </LinearLayout> 

Vertical Linear Layout

Also now look at gravity, when you apply gravity to a texture, TEXT is centered in the text view (if gravity = center)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" **android:gravity="center"** android:text="Second Activity" /> </LinearLayout> 

centerTextVertical

Now you can understand that gravity sets the gravity for the content inside the widget. The "content" of a widget may be widgets themselves. LinearLayout is a widget, and in the linear layout you add other types of widgets (button and text view) Thus, applying the gravity = center to textview method sets its content (i.e. text) in the center and applying gravity to the linearlayout will center it content (i.e. Textview) in the center

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:gravity="center" android:text="Second Activity" /> </LinearLayout> 

layout_gravity_center

0
source

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


All Articles