<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"即可实现按钮居中效果
本文介绍了一种在Android应用开发中使按钮居中的布局方法。通过在LinearLayout中设置android:gravity=center属性,可以实现在父容器内的居中显示。此方法适用于需要对界面元素进行精确布局的应用场景。
749

被折叠的 条评论
为什么被折叠?



