【问题标题】:view not snapped to top on LinearLayout视图未在 LinearLayout 上对齐顶部
【发布时间】: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


    【解决方案1】:

    我参考了一些文档和 SO 线程来寻找解决方案,因为这个问题对我来说似乎很有趣。

    终于找到原因了。

    默认情况下,水平的LinearLayout 对齐其所有子控件的基线。因此,多行按钮中的第一行文本与其他按钮中的单行文本垂直对齐。

    要禁用此行为,请在 LinearLayout 上设置 android:baselineAligned="false"

    这个答案的所有功劳归于@Karu:https://stackoverflow.com/a/8290258/4211264

    【讨论】:

      【解决方案2】:

      这是由于第一个按钮中的文本换行造成的。其他两个按钮在屏幕上占用了太多的匹配空间。第一个按钮尝试通过自动包装留在屏幕上。我不知道你的任务是什么。如果您想将按钮保留在一行中,请尝试在所有按钮上使用 layout_weight = 1。

      【讨论】:

      • 嗨。谢谢您的答复。高度设置为换行。所以这不应该垂直拉伸按钮直到它适合吗?如果是这样的话,为什么这会从上面得到一个边距,因为这与它的宽度无关
      【解决方案3】:

      这是因为您为每个按钮赋予了不同的权重。你需要给每个按钮同等的权重。用这个替换你的布局文件代码。

      <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="1"
          android:text="Button" />
      
      <Button
          android:id="@+id/button"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="Button" />
      

      【讨论】:

      • 嗨。谢谢您的答复。但这会给所有直接孩子提供相同的“宽度”。我的问题是它从顶部获得的边距无处可寻。
      • OP 已询问第一个按钮顶部的额外边距的原因。并且根据要求,可能需要为所有小部件赋予不同的权重
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2015-12-16
      • 2013-10-12
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2017-01-28
      相关资源
      最近更新 更多