There is a part of the markup:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/upperLayout" android:layout_above="@id/button2"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:text="@string/default_text"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:text="@string/text_like_button" tools:ignore="NestedWeights" /> </LinearLayout> </LinearLayout> 

It has a problem: the button, for some reason it is not centered vertically, although android: gravity = "center_vertical". enter image description here

What is the problem?

  • I recommend that you use ConstraintLayout instead of LinearLayout. Firstly, it is faster at work, secondly in the interface layout, in the third it is not good to wet LinearLayout in LinearLayout - Astend Sanferion February

1 answer 1

android:gravity is responsible for aligning TEXT inside the button.

Use android:layout_gravity is the "external" gravity of the button itself.

And a small note not to the point: if you put weight (for LinearLayout ), then height/width (depending on the orientation of the parent) is better to set = 0dp

  • not "better to put", but it is necessary to put - pavlofff