【问题标题】:android tablelayout fixed columns - not getting desired resultsandroid tablelayout固定列 - 没有得到想要的结果
【发布时间】:2015-04-17 20:09:37
【问题描述】:

我是 Android 开发新手。

我尝试过使用LinearLayout,现在终于使用TableLayout

我需要一个带有 3 列的简单固定可滚动表,但我无法获得单个列表行来执行我需要的操作...我有 ListViewListActivity 及其 Adapter 排序。

基本上我想要:

  • 3 列,所有固定宽度,但调整到不同的屏幕 大小(因此是百分比?)
  • 第一列有标题,应该是最大的。如果文本超出空间,则将其省略
  • 第二列是价格
  • 第三列是一个图标,应该在最右边
|-----------------------------------------|
| This title exceeds th....|  $500 | Icon |
| This title fits          | $1000 | Icon |
| Other title              |   $25 | Icon |
| Another long title th... | $1200 | Icon |
|-----------------------------------------|

这是我最新的 xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow>
        <TextView
            android:id="@+id/title"
            android:layout_height="45dip"
            android:layout_width="wrap_content"
            android:layout_marginRight="10dip"
            android:layout_weight="6"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="A long Title it is"
            android:textSize="24sp" />


        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="45dip"
            android:layout_weight="3"
            android:layout_marginRight="10dip"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Price"
            android:textSize="24sp" />

        <ImageButton
            android:layout_weight="1"
            android:id="@+id/cart"
            android:gravity="end"
            android:src="@drawable/cart"
            android:layout_width="45dp"
            android:layout_height="30dp" >
        </ImageButton>
    </TableRow>
</TableLayout>

结果有时候图标不在最右边,标题文字没有固定宽度……

我搜索了很多问题并尝试了很多东西(请参阅layout_weight),但到目前为止似乎没有奏效。谢谢。

【问题讨论】:

    标签: android android-layout layout tablelayout


    【解决方案1】:

    在表格行(或线性布局的水平方向)中,建议在处理android:layout_weight时使用android:layout_width="0dp"

    您能否尝试将 layout_width 属性设置为 0dp。我认为它会起作用。

    【讨论】:

    • 很遗憾没有。现在我只看到第一列的前两个字符,其余的由于某种原因消失了
    • 你的意思是标题栏中的文字?
    • 我已经导入了你的布局。只需将宽度值设置为 0dp,对我来说就完美了。您可以尝试增加标题的权重值吗?
    • 是的,在标题列中,对于所有行,我现在只看到前两个字母,然后什么也没有,就像阴影会覆盖所有内容一样。我将标题列的权重增加到 20 - 没有效果。我觉得这很愚蠢......
    • 其他列的宽度也设置为 0dp 对吧?
    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    相关资源
    最近更新 更多