添加动态TableLayout在一个Android应用程序边界 [英] Adding a dynamic TableLayout with borders in a Android App

查看:164
本文介绍了添加动态TableLayout在一个Android应用程序边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android应用程序。这个Android应用程序将是动态的对象。这些对象的地方一个地址或纬度/经度,和距离从当前的位置,和一个ETA。我想要做的是增加与边框上的 TableLayout 的对象,但我需要能够动态地添加行为地方增加的数量。

我有所了解如何做到这一点的固定硬盘codeD上的XML项目的数量,但什么是最好的方式,当对象的数量是从Activity.java文件来吗?

下面是一个截图 TableLayout 我想:

所以,对象将是一个地址,距离和方向的地方。

解决方案
  

但我需要能够动态地添加行为的名额增加。

这并不难,当你有一个新的对象追加的TableRow 与数据的 TableLayout

  

我有所了解如何做到这一点的固定硬盘codeD项目上的XML数字,但会是什么时候对象的数量从Activity.java文件?

未来的最好方式

我不觉得有什么(你认为最好的方法是什么,或)一个最好的办法。您可以:

  • 插入假的观点,作为分隔。这将是更容易实现可视化,但它也将增加你的应用程序的内存消耗,不良后果,如果行的数量大。 (1)
  • 或者使用可绘制为背景的模拟边界(如九补片图像)。这将是简单的,然后插入另外的观点,但你需要更多的人才,使它看起来很好。 (2)

有关图像的一些例子:

(1)

 私有静态最终诠释DIVIDER_SIZE = 2;

// rowsCount行数要添加到TableLayout
私人无效buildOldSchool(TableLayout表,INT rowsCount){
    查看除法器;
    的for(int i = 0; I< rowsCount;我++){
        的TableRow行=新的TableRow(本);
        为(诠释J = 0; J&小于7; J ++){
            如果(j%2 == 0){
                分=新的视图(本);
                divider.setLayoutParams(新TableLayout.LayoutParams(
                        DIVIDER_SIZE,TableLayout.LayoutParams.MATCH_PARENT));
                divider.setBackgroundColor(Color.GREEN);
                row.addView(除法,新TableRow.LayoutParams(
                        DIVIDER_SIZE,TableRow.LayoutParams.MATCH_PARENT));
                继续;
            }
            TextView的电视=新的TextView(本);
            tv.setText(DX); //虚拟数据
            row.addView(电视,新TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
        }
        分=新的视图(本);
        divider.setLayoutParams(新TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,DIVIDER_SIZE));
        divider.setBackgroundColor(Color.GREEN);
        如果(我== 0){
            table.addView(分);
            分=新的视图(本);
            divider.setLayoutParams(新TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,DIVIDER_SIZE));
            divider.setBackgroundColor(Color.GREEN);
        }
        table.addView(行);
        table.addView(分);
    }
}
 

(2)或带有图像:

 私人无效buildWithDrawables(TableLayout表,INT rowsCount){
    的for(int i = 0; I< rowsCount;我++){
        的TableRow行=新的TableRow(本);
        row.setBackgroundResource(我== 0?R.drawable.firstrow
                :R.drawable.normalrow);
        为(诠释J = 0; J&所述; 3; J ++){
            TextView的电视=新的TextView(本);
            tv.setBackgroundResource(j == 2?R.drawable.extra
                    :R.drawable.cell);
            tv.setText(DX);
            row.addView(电视,新TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
        }
        table.addView(行);
    }
}
 

如果图像是:

  • R.drawable.cell

  • R.drawable.extra (视觉透明绘制它复制九补丁以上):

  • R.drawable.normalrow

  • R.drawable.firstrow

忽略我的设计技巧。

如果您预见到大量的行我会建议你使用的ListView ,你可以将pretty的容易使它看起来像有边框的表格

I am creating a Android app. This Android App will have objects that are dynamic. These objects are Places with a Address or Lat/Long, and distance from current location, and a ETA. What I would like to do is add with objects on a TableLayout with borders, but I need to be able to dynamically add rows as the number of places increase.

I understand somewhat how to do this for a fixed hardcoded number of items on the xml, but what would be the best way when the number of objects is coming from the Activity.java file?

Below is a screenshot of the TableLayout I would like:

So the object would be a place with a address, distance and direction.

解决方案

but I need to be able to dynamically add rows as the number of places increase.

This isn't difficult, when you have a new object append a TableRow with the data to the TableLayout.

I understand somewhat how to do this for a fixed hardcoded number of items on the xml, but what would be the best way when the number of objects is coming from the Activity.java file?

I don't think there is a best way (or what you consider best way). You either:

  • Insert fake views to act as dividers. This would be easier to implement visually but it will also increase the memory consumption of your app, with bad consequences if the number of rows is big. (1)
  • Or use drawables for the backgrounds to simulate the borders (like nine-patch images). This would be simpler then inserting additional views but you need a bit more talent to make it look well. (2)

Some examples for your image:

(1)

private static final int DIVIDER_SIZE = 2;

// rowsCount the number of rows to add to the TableLayout
private void buildOldSchool(TableLayout table, int rowsCount) {
    View divider;
    for (int i = 0; i < rowsCount; i++) {
        TableRow row = new TableRow(this);
        for (int j = 0; j < 7; j++) {
            if (j % 2 == 0) {
                divider = new View(this);
                divider.setLayoutParams(new TableLayout.LayoutParams(
                        DIVIDER_SIZE, TableLayout.LayoutParams.MATCH_PARENT));
                divider.setBackgroundColor(Color.GREEN);
                row.addView(divider, new TableRow.LayoutParams(
                        DIVIDER_SIZE, TableRow.LayoutParams.MATCH_PARENT));
                continue;
            }
            TextView tv = new TextView(this);
            tv.setText("DX"); // dummy data
            row.addView(tv, new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
        }
        divider = new View(this);
        divider.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT, DIVIDER_SIZE));
        divider.setBackgroundColor(Color.GREEN);
        if (i == 0) {
            table.addView(divider);
            divider = new View(this);
            divider.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, DIVIDER_SIZE));
            divider.setBackgroundColor(Color.GREEN);
        }
        table.addView(row);
        table.addView(divider);
    }
}

(2) or with images:

private void buildWithDrawables(TableLayout table, int rowsCount) {
    for (int i = 0; i < rowsCount; i++) {
        TableRow row = new TableRow(this);
        row.setBackgroundResource(i == 0 ? R.drawable.firstrow
                : R.drawable.normalrow);
        for (int j = 0; j < 3; j++) {
            TextView tv = new TextView(this);
            tv.setBackgroundResource(j == 2 ? R.drawable.extra
                    : R.drawable.cell);
            tv.setText("DX");
            row.addView(tv, new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
        }
        table.addView(row);
    }
}

Where the images are:

  • R.drawable.cell:

  • R.drawable.extra (a visually transparent drawable which replicates the nine-patch above):

  • R.drawable.normalrow:

  • R.drawable.firstrow:

Ignore my design skills.

If your foresee a large number of rows I would advise you to use a ListView, which you could pretty easy make it to look like a table with borders.

这篇关于添加动态TableLayout在一个Android应用程序边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆