为什么这些观点不一致?

发布于 2024-09-30 18:10:43 字数 1575 浏览 1 评论 0原文

我不明白为什么我需要做才能让 TableLayout 中的视图完美对齐。

我有一个与此类似的布局:

<TableLayout android:id="@+id/keyboard"
     android:layout_width="fill_parent"
     android:layout_height="378px">
    <TableRow android:layout_span="5" >
      <TextView android:id="@+id/button20"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="10"
          android:text="L1" />
      <TextView android:id="@+id/button21"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L2" />
      <TextView android:id="@+id/button22"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L3" />
      <TextView android:id="@+id/button23"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L4" />
      <TextView android:id="@+id/button24"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L5" />
    </TableRow>
</TableLayout>

现在,在绘制时,第一个 TextView 显示的位置比所有其他 TextView 低 2 个像素。 重写 onLayout 方法;我看到第一个视图正在调用 onLayout: 顶部 = 2 ;底部 = 44 ;而其他所有的都是 top = 0 ;底部 = 42。

只要字体大小与其他字体稍有不同,就会发生这种情况;或者如果文本换行到另一行(因为它不适合宽度)。 所有 TextView 具有相同的尺寸;但有些显示的略低于另一个。

如果有区别的话,此布局位于RelativeLayout 内。

我尝试了很多不同的组合,但总是会出现一些差异。 任何帮助将不胜感激。

谢谢 ! 杰宇

I can't figure out why I need to do to get my views in a TableLayout to align perfectly.

I have a layout similar to this:

<TableLayout android:id="@+id/keyboard"
     android:layout_width="fill_parent"
     android:layout_height="378px">
    <TableRow android:layout_span="5" >
      <TextView android:id="@+id/button20"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="10"
          android:text="L1" />
      <TextView android:id="@+id/button21"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L2" />
      <TextView android:id="@+id/button22"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L3" />
      <TextView android:id="@+id/button23"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L4" />
      <TextView android:id="@+id/button24"
          android:layout_width="63px"
          android:layout_height="42px"
          android:textSize="12"
          android:text="L5" />
    </TableRow>
</TableLayout>

Now, when drawn, the first TextView is displayed 2 pixels lower than all the other ones.
Overriding the onLayout method ; I see that onLayout is being called for the first View with:
top = 2 ; bottom = 44 ; while all the other ones are top = 0 ; bottom = 42.

This happens whenever the font size is slightly different than the others ; or if the text wrap on another line (because it doesn't fit within the width).
All TextViews have the same dimensions ; but some are displayed slightly lower than the other.

This layout is within a RelativeLayout if that makes a difference.

I tried a lot of different combination, and there's always some discrepancies happening.
Any help will be greatly appreciated.

Thanks !
JY

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓬勃野心 2024-10-07 18:10:43

看起来文本视图在其基线上对齐。因此,如果文本较小,则文本视图内的基线会较高。因此,文本视图将放置在较低的位置,以匹配其他具有较高文本的文本视图的基线。

为了防止行中的基线对齐,请尝试将 TableRow 行更改为:

<TableRow android:layout_span="5" android:baselineAligned="false" >

缺点当然是文本看起来可能不再对齐。

It looks like the textview's are aligned on their baselines. So if a text is smaller its baseline will be higher inside the textview. Therefore the textview will be positionned lower to match the baseline of other textviews with taller text.

To prevent baseline alignment in the row, try to change the TableRow line to:

<TableRow android:layout_span="5" android:baselineAligned="false" >

The disadvantage is of course that texts may not look aligned anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文