首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展tablerow和膨胀xml tablerow的麻烦

扩展tablerow和膨胀xml tablerow的麻烦
EN

Stack Overflow用户
提问于 2012-04-16 15:54:44
回答 3查看 1.6K关注 0票数 2

我已经非常接近LinearLayout的替代品了,但是做不到这一点有点让人恼火。为了获得最大的灵活性,我定义了一个只定义了行头的TableLayout xml。接下来,我生成了一个单独的定义“行模板”的TableRow。在Javacode中,我继承了TableRow的子类,并在构造函数中膨胀了tablerow模板以附加到根(子类化的类)。

好吧,到目前为止还不错。当填充表时,头行是ok的,但其他行不是。它们似乎是以不同的方式布局的,两列没有像预期的那样填满整个宽度,因此两列没有正确地彼此对齐。

有谁能解释一下这个问题吗?我尝试了很多解决方案,但都不管用。

带有标题行的表格布局

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <TableLayout
            android:id="@+id/zone_table"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:stretchColumns="*" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="fill_horizontal"
                android:clipToPadding="false" >

                <TextView
                    android:layout_width="0dip"
                    android:layout_weight="0.8"
                    android:background="#ffcccccc"
                    android:text="Zonename"
                    android:textColor="@android:color/black" />

                <TextView
                    android:layout_width="0dip"
                    android:layout_weight="0.2"
                    android:background="#ffcccc00"
                    android:gravity="right"
                    android:text="Antall"
                    android:textColor="@android:color/black" />
            </TableRow>
        </TableLayout>
    </HorizontalScrollView>

</ScrollView>

“其他”膨胀的行

代码语言:javascript
复制
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/zonetablerow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>

    <TextView
        android:id="@+id/zonerow_name"
        android:layout_width="0dip"
        android:layout_weight="0.8"
        android:background="#ffcccccc"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/zonerow_invcount"
        android:layout_width="0dip"
        android:layout_gravity="right"
        android:layout_weight="0.2"
        android:background="#ffcccc00"
        android:textSize="18dp" />

</TableRow>

扩展TableRow的类

代码语言:javascript
复制
    public class ZoneRow extends TableRow {
    private ZoneInventoryDAO dao = null;
    private int inventoryCount = 0;

    public ZoneRow(Context ctx, ZoneInventoryDAO dao) {
        this(ctx, dao, 0);
    }

    public ZoneRow(Context ctx, ZoneInventoryDAO dao, int inventoryCount) {
        super(ctx);
        setWeightSum(1.0f);
        this.dao = dao;
        this.inventoryCount = inventoryCount;
        doLayout();
    }

    private void doLayout() {
        // XML layouten settes med zonerow som parent (se:
        // http://developer.android.com/resources/articles/layout-tricks-merge.html)
        View v = LayoutInflater.from(getContext()).inflate(R.layout.zonerow,
                this, true);
        TextView t = (TextView) findViewById(R.id.zonerow_name);
        TextView cnt = (TextView) findViewById(R.id.zonerow_invcount);

        t.setText(dao.getZoneAlias());
        cnt.setText(String.valueOf(inventoryCount));

    }

    public void incInventory() {
        inventoryCount++;
    }

    public ZoneInventoryDAO getDAO() {
        return dao;
    }

}
EN

回答 3

Stack Overflow用户

发布于 2012-04-20 00:23:02

看起来您正在扩展tablerow并创建该对象的一个实例。这将导致创建一个表行。然后,您正在膨胀另一个表,由于某种原因,它与第一个表相互作用。为了快速解决问题,可以尝试添加类似table.setColumnStretchable(0,true)的代码;

票数 0
EN

Stack Overflow用户

发布于 2013-05-02 07:15:22

我也有同样的问题。我通过不这样做来修复它-特别是通过不对TableRow进行子类化,而只是在代码中实例化它们,并手动应用我需要的任何东西。您可以通过封装来实现这一点。有一些包含ZoneInventoryDAO、inventoryCount和tableRow的记录,它们指向相应的TableRow实例。只需使用膨胀实例化TableRow:

代码语言:javascript
复制
TableLayout table = (TableLayout) inflater.inflate(R.layout.my_table, null); // etc
...
for (//each data item//) {
  TableRow tr = (TableRow) inflater.inflate(R.layout.my_table_row, null);
  TextView tv1 = (TextView) findViewById(R.id.table_entry_1);
  tv1.setText("Whatever goes here");
  ...
  // and so on for each column
  table.addView(tr);
}

当我将相同的代码移动到扩展TableRow的类中时,上面的方法对我来说是有效的。

票数 0
EN

Stack Overflow用户

发布于 2016-04-09 01:24:13

我知道这是旧的,但我也有同样的问题,并解决了它,所以也许这将有助于未来的用户。

问题是,当您从XML中膨胀TableRow,并将其添加到扩展的TableRow中时,表将其视为具有多个子项的单个列,因为布局中的TableRow本质上是一个ViewGroup。因此,它将表布局为单列表。

解决方案是在TableRow XML布局中使用<merge>类型,而不是<TableRow>类型。这样,在膨胀时,xml中的项将合并到扩展的TableRow中,并且扩展的TableRow将有多列。

以下是示例代码

代码语言:javascript
复制
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/zonetablerow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>

    <TextView
        android:id="@+id/zonerow_name"
        android:layout_width="0dip"
        android:layout_weight="0.8"
        android:background="#ffcccccc"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/zonerow_invcount"
        android:layout_width="0dip"
        android:layout_gravity="right"
        android:layout_weight="0.2"
        android:background="#ffcccc00"
        android:textSize="18dp" />

</merge>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10170447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档