开发者

Why aren't those views aligned?

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. Overridi开发者_StackOverflow社区ng 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜