<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Read Books XML"
    android:layout_gravity="center_horizontal"/>

<TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

在 Android 布局中,可以通过添加 android:layout_gravity="center_horizontal" 属性来将按钮水平居中。该属性可以应用于多种布局元素,例如 Button、TextView 等。

此外,还可以使用其他方式来居中元素,例如:

  • 使用 android:gravity 属性来设置元素内容的居中对齐方式。
  • 使用 android:layout_margin 属性来设置元素的边距,从而实现间距和居中。
  • 使用 android:layout_weight 属性来分配布局空间,从而实现元素的居中显示。

请根据您的具体需求选择合适的居中方式。

Android LinearLayout 中如何居中按钮

原文地址: https://www.cveoy.top/t/topic/nDk2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录