Android Button按钮居中

<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>

这里不能只在Button属性中写Android:layout_gravity="center",因为这只能表示在父类容器方向上的居中,所以需要在父类容器LinearLayout中加入

android:gravity="center"

即可实现按钮居中效果

猜你喜欢

转载自blog.csdn.net/leng15183596537/article/details/80081541