按钮边距随按钮文本内容改变

3

让我们从有趣的部分开始,这是问题的图形。水平方向上,一切都很美好。

凹陷按钮 中间的按钮,我想让它与其他三个按钮对齐。以下是基本信息:

  1. 总体上,它是一个相对布局
  2. 在这个相对布局内,有一个水平线性布局,包含三个按钮
  3. 中间按钮的“凹陷”与其成为双行文本完全相关,如果我将其更改为单行,则会正确对齐
  4. 指定的按钮高度与下沉无关,即使将其当前大小(从当前70到170)增加到两倍以上,也会显示完全相同的行为(和行为大小)
  5. “custom_button”背景没有影响,如果我将它们全部更改为没有背景,看起来像股票的按钮,同样的定位会发生

这里是XML(仅限相对布局内的线性布局):

<LinearLayout 
android:id="@+id/wideButtons"
android:layout_below="@+id/buttonClockFinish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:orientation="horizontal">

    <Button
    android:id="@+id/buttonLog"
    android:layout_weight="1"
    android:layout_height="70dp"
    android:padding="0dp"
    android:layout_marginRight="3dp"
    android:layout_width="fill_parent"
    android:background="@drawable/custom_button"
    android:text="View Log" />

    <Button
    android:id="@+id/buttonLocation"
    android:layout_weight="1"
    android:layout_height="70dp"
    android:padding="0dp"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:layout_width="fill_parent"
    android:background="@drawable/custom_button"
    android:text="Location\nD1-RS" />

    <Button
    android:id="@+id/buttonHelp"
    android:layout_weight="1"
    android:layout_height="70dp"
    android:padding="0dp"
    android:layout_marginLeft="3dp"
    android:layout_width="fill_parent"
    android:background="@drawable/custom_button"
    android:text="Help" />

</LinearLayout>

那么为什么它不能对齐呢?
1个回答

8

我正准备发布这个问题,最后进行了一次实验。我在按钮上添加了第三行文本。这使它进一步向下移动。但我意识到的是,中间按钮顶部行的文本与其两侧按钮的文本完美对齐。

所以问题不是内部边距有问题,无法将文本压缩到顶部边框。对齐是文本而不是按钮图形。

一直以来,我认为存在某些神秘的“填充”,我没有将其清零,但是当按钮文本有三行时,它非常高兴只有大约1dp的填充。

解决方案是添加:

android:layout_gravity="center_vertical"

添加了一个按钮。为了保持一致性,我也把它添加到其余的按钮中。

教训:当你认为事情没有对齐时,也许实际上已经对齐了,只是可能你看错了东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接