【问题标题】:button placement in a linear layout线性布局中的按钮放置
【发布时间】:2012-04-20 22:19:36
【问题描述】:

我有一个线性布局,里面有几个按钮。按钮图像大小相同,属性相同……除了一个按钮。这一个按钮的字体较小。除了这个之外,所有的按钮都按照我想要的方式完美地排列在一起。出于某种原因,字体较小的按钮在屏幕上的显示位置比其他按钮稍低。我很难理解按钮需要更少空间占用更多空间的想法。

有人可以给我一些提示吗?

编辑

这是 main.xml(好像 SO 过滤了其中的一部分,所有重要的东西都在这里...)

<ScrollView android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="300px">

    <TextView
            android:id="@+id/the_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textSize="9pt"
            android:background="@color/paper"
            android:paddingLeft="20dp"
            android:paddingBottom="20dp"
            android:textColor="@color/type"
            />
</ScrollView>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <Button style="@style/ASR33_button"
            android:tag="Y"
            android:text="Y"
            />

    <Button style="@style/ASR33_button"
            android:tag="N"
            android:text="N"
            />

    <Button style="@style/ASR33_button"
            android:tag="E"
            android:text="E"
            />

    <Button style="@style/ASR33_button"
            android:tag="W"
            android:text="W"
            />

    <Button style="@style/ASR33_button"
            android:tag="S"
            android:text="S"
            />

    <Button style="@style/ASR33_button"
            android:tag="F"
            android:text="F"
            />

    <Button style="@style/ASR33_button"
            android:tag="R"
            android:text="R"
            />

    <Button style="@style/ASR33_button"
            android:tag="M"
            android:text="M"
            />

    <Button style="@style/ASR33_button"
            android:tag="T"
            android:text="T"
            />

    <Button style="@style/ASR33_button"
            android:onClick="onEnterButtonClicked"
            android:textSize="6pt"
            android:text="RE-\nTURN"
            />

    <Button style="@style/ASR33_button"
            android:tag="U"
            android:text="U"
            />
</LinearLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView
            android:id="@+id/instructions"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="9pt"
            android:paddingLeft="10dp"
            android:typeface="normal"
            android:text="Commands: (Y)es, (N)o, (N)orth, (E)ast, (W)est, (S)outh, (M)ap, (ST)atus, (Fight), (R)un, (SU)icide.  All commands must be followed by RETURN."
            />

</LinearLayout>

奇怪的是倒数第二个,带有不同的 onclick 事件。该样式的字符大小为 11pt。如果我使用它(和一个 1 个字母的按钮名称,就像其他人一样)它的行为。但这不是 ASR33 的“输入”键。因此,如果我将字体大小减小到 6 pt,就会发生奇怪的事情。

样式可见here

再次,请阅读参考资料或想法,如果我有一两个词要搜索,我可以弄清楚。很难知道你不知道什么......

分辨率

Anurag 说得对,请参阅下面的答案。以下是更新后的 LinearLayout 的摘录:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">

【问题讨论】:

  • 如何将&lt;TableLayout&gt; 中的组件添加到&lt;TableRow&gt; 标签中?

标签: android android-button android-linearlayout


【解决方案1】:

可能由于按钮的 wrap_content 属性而发生了调整大小。所以你应该做的是在线性布局中设置一个固定的高度来容纳所有按钮,同时它的 with 设置为填充父级。

在线性布局中,让单个按钮设置高度以包裹内容,这将使所有按钮的高度与线性布局的高度相同,并为小按钮设置属性android:adjustViewBounds="true"。此属性将调整您的图像按钮的大小以保持纵横比。我希望这会有所帮助。

编辑: 所以这是您的问题的解决方案,这是由于线性布局的基本对齐属性引起的。默认情况下,水平 LinearLayout 对齐其所有子控件的基线。因此,多行按钮中的第一行文本与其他按钮中的单行文本垂直对齐。在 LinearLayout 上设置 android:baselineAligned="false"。这在我的 HTC 上完美运行。

【讨论】:

  • 按钮都共享相同的背景图像,所以我希望它们的大小都相同,只要文本被完全包含。
  • 我认为这不是问题。如果您按照我提到的方式进行操作,背景图像仍将保持成比例。
  • @AnarugRamdasan 我很欣赏你所说的,但我真的很想理解为什么我的失败。否则我下周会回来问类似的问题。
  • 哦,我刚刚注意到了编辑。我对这个有点兴趣。你能把你得到的输出截图吗?
  • 你让我对这个问题很着迷:P 无论如何我很高兴能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
相关资源
最近更新 更多