<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context="com.example.xuhao.tongzhiapplication.MainActivity" android:weightSum="1"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
You can't just write Android:layout_gravity="center" in the Button attribute here, because this can only mean centering in the direction of the parent container, so you need to add it to the parent container LinearLayout
android:gravity="center"
Button centering effect can be achieved