我有下面的XML文件安卓重力=“中心”工作不正常
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="100dp"
android:orientation="vertical"
android:background="#ff0000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="Button"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
它具有以下设计。
不应在button
被放置在中心垂直以及?我知道layout_gravity
和gravity
是如何工作的。所以根据我的理解,按钮应该在水平方向和垂直方向都处于中心位置。
这工作正常。谢谢。但是在linearLayout中不添加gravity =“center”,它如何将按钮放置在水平中心? – thedarkpassenger
你是什么意思?你不能用其他方式做。由于LinearLayout将方向上的子视图放置在一个方向上,因此您必须设置重力,以便了解它必须放置的位置 –