【发布时间】:2021-05-28 10:25:50
【问题描述】:
我有一个 LinearLayout,它的 XML 是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button" />
</LinearLayout>
结果是:
您可以看到左上角的按钮距顶部有一点边距,但代码显示没有边距。 为什么会这样?
还有一个奇怪的解决方案,如果您将gravity:top 设置为所有按钮,您将获得预期的结果。但是为什么甚至需要它,因为linearlayout(horiz) 应该开始从左上角到右上角添加项目。
【问题讨论】:
标签: android android-linearlayout