【问题标题】:TableLayout Cell PositioningTableLayout 单元格定位
【发布时间】:2012-05-30 03:16:27
【问题描述】:

我一直在尝试将我的单元格排列在下面的表格行中,以使第二个单元格的值(带有背景图像的按钮)紧邻第一个单元格的文本右侧。下面当前拉伸单元格 2 中的图像,看起来像 。

关于如何停止拉伸并将按钮图像放置在单元格 2 的最左侧的任何想法?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <TableLayout 
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:stretchColumns="1"
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp">
        <TableRow>
        <TextView 
            android:id="@+id/timeMainLabel" 
            android:layout_weight="1"
            android:textSize="14sp"
            android:layout_column="0" 
            android:padding="1dip" 
            android:text="cell one texxxxxxxxt"/>
        <Button
            android:id="@+id/pdm_tooltip_btn1"
            android:layout_column="1" 
            android:background="@drawable/tooltip_btn" />
        <TextView 
            android:layout_column="2" 
            android:id="@+id/timeLabel"
            android:textSize="14sp" 
            android:text="cell 3"
            android:gravity="right" 
            android:padding="1dip" 
            android:visibility="visible" 
            android:layout_weight="1"/>
    </TableRow>
    <TableRow>
        <TextView 
            android:id="@+id/timeMainLabel" 
            android:layout_weight="1"
            android:textSize="14sp"
            android:layout_column="0" 
            android:padding="1dip" 
            android:text="cell one text"/>
        <Button
            android:id="@+id/pdm_tooltip_btn1"
            android:layout_column="1" 
            android:background="@drawable/tooltip_btn" />
        <TextView 
            android:layout_column="2" 
            android:id="@+id/timeLabel"
            android:textSize="14sp" 
            android:text="cell 3"
            android:gravity="right" 
            android:padding="1dip" 
            android:visibility="visible" 
            android:layout_weight="1"/>
    </TableRow>
    </TableLayout>
</LinearLayout>

更新的图像:

【问题讨论】:

    标签: android


    【解决方案1】:

    这需要一些摆弄。基本上,您必须在第 1 列之后添加一些空列(这将是第二列,因为它们是零索引的)。

    所以你将拥有:

    <TableRow>
       <TextView layout_column="0">
    
       <Button layout_column="1">
    
       <TextView layout_column="2"> // DUMMY!
    
       <TextView layout_column="3">
    </TableRow>
    

    现在,您可以随意使用weight 并进行相应的设置。

    第三列(第 2 列)将填充两个元素之间的空间,因此可以根据需要进行拉伸。

    【讨论】:

    • 感谢您的回复。我放入了一个具有一定宽度的虚拟文本视图来推动图像列,但我无法将它放在“已发送消息”列的文本旁边(请参阅更新的图像)。看起来该单元格延伸到最长文本的长度。有什么方法可以在发送的消息单元格内移动图像?
    • 如果它们都在一个单独的 TableRow 元素中,一个不会影响另一个单元格的宽度。这种行为在其他方面是无法解释的,因为一行不会以任何方式影响另一行的列。
    • 我建议你编辑这个问题,这样我就可以编辑这个答案,这样它就可以与任何尝试谷歌搜索相同内容的人相关。你的问题可能只对我有意义 :-) 或者只是提出一个新问题之类的。
    • 我看到一个单元格的宽度会影响同一列中的其他元素,并添加了一个示例。对于我最初提出的问题,我还没有真正的解决方案。使用虚拟间隔柱并没有让我一路走好。
    • @c12 在 TableLayout 元素中禁用stretch_columns 会发生什么?我从未见过不同的行会影响彼此的列。
    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    相关资源
    最近更新 更多