android - 按钮边距随按钮文本内容而变化

标签 android xml button layout

让我们从有趣的部分开始,这是麻烦的图形。横向来看,一切都是美好的。

Sunk button 中间的按钮,我想与其他三个按钮对齐。以下是基础知识:

  1. 总的来说,这是一个相对布局
  2. 在这个相对布局中,它是一个水平线性布局,包含三个按钮
  3. 中间按钮的“下沉”与它是双行文本 100% 相关,如果我将它更改为单行,它会正确对齐
  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>

那么它到底为什么不对齐呢?

最佳答案

我正要发布这个问题,并做了最后一个实验。我向按钮添加了第三行文本。这进一步压低了它。但我意识到它的共同点是中间按钮顶行的文本与它两侧的两个按钮的文本保持完美对齐。

所以这并不是因为内部边距有问题,无法将文本压在顶部边框上。 对齐的是文本,而不是按钮图形。一直以来我都认为有一些神秘的 :padding 我没有取消,但是三行按钮文本很高兴只有大约 1dp 左右的填充。

解决方案是添加

android:layout_gravity="center_vertical"

到那个按钮。为了保持一致性,我也将它添加到其余部分。

教训:当您认为事情不一致时,也许它们实际上是一致的,但也许您看错了东西。

关于android - 按钮边距随按钮文本内容而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18188180/

相关文章:

xml - 封装的签名究竟改变了什么?

java - 如何从另一种方法停止/中断正在运行的线程?

java - Android 和 AppEngine 网络服务 : Json. .. RPC、REST... Protocol Buffer ?

html - 从 .HTML 文件链接到 .XSL 文件

c# - 如何在 C# 中调用事件方法?

java - 来自 9patches WITHOUT XML 的自定义按钮

android - 背景为 Android 中的 AnimationDrawable 的按钮状态

android - 如何为所有分辨率创建 Android 应用程序?

android - 布局方向在 ConstraintLayout 中不起作用

c# - 使用 OpenXML 时以正确的顺序插入子元素