两个dinamically添加TableRow的列具有相同的宽度?

时间:2017-11-03 17:25:19

标签: android android-layout

我正在编写一个通过查询读取sqlite数据库的应用程序,并编译一个表,其结果为查询返回的每一行添加了一个TableRow布局。

目前,我有两张桌子,一张用于固定标题和水平滚动用途,另一张用于结果。
但是两者的列没有相同的宽度 逻辑解决方案是从标题中获取列的宽度,从结果中获取宽度,并将较低的值设置为另一个的值。
但是我的代码没有按预期工作。

如何设置列witdh?

这是我的代码:

final int[] maxLarg = new int[4];
final TableRow testata = findViewById(R.id.testRigaTabOrdini);
final TableRow dettagli = findViewById(R.id.dettRigaTabOrdini);
final TextView testCodCli = testata.findViewById(R.id.testCodice);
final TextView testRagSoc = testata.findViewById(R.id.testRagSoc);
final TextView testDataDoc = testata.findViewById(R.id.testData);
final TextView testImporto = testata.findViewById(R.id.testImporto);

final TextView codCli = dettagli.findViewById(R.id.dettCodCli);
final TextView ragSoc = dettagli.findViewById(R.id.dettRagSoc);
final TextView dataDoc = dettagli.findViewById(R.id.dettDataDoc);
final TextView importo = dettagli.findViewById(R.id.dettImporto);
final int[] lungh = new int[4];

testata.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        maxLarg[0] = testCodCli.getWidth();
        maxLarg[1] = testRagSoc.getWidth();
        maxLarg[2] = testDataDoc.getWidth();
        maxLarg[3] = testImporto.getWidth();
            testata.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

dettagli.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        lungh[0] = codCli.getWidth();
        lungh[1] = ragSoc.getWidth();
        lungh[2] = dataDoc.getWidth();
        lungh[3] = importo.getWidth();

            dettagli.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

    ViewTreeObserver.OnDrawListener drawListener = new ViewTreeObserver.OnDrawListener() {
        @Override
        public void onDraw() {
            for (int i = 0; lungh.length > i;){
                if (lungh[i] > maxLarg[i]){
                    switch (i){
                        case 0:
                            codCli.setWidth(lungh[i]);
                            testCodCli.setWidth(lungh[i]);
                            break;
                        case 1:
                            ragSoc.setWidth(lungh[i]);
                            testRagSoc.setWidth(lungh[i]);
                            break;
                        case 2:
                            dataDoc.setWidth(lungh[i]);
                            testDataDoc.setWidth(lungh[i]);
                            break;
                        case 3:
                            importo.setWidth(lungh[i]);
                            testImporto.setWidth(lungh[i]);
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (i){
                        case 0:
                            codCli.setWidth(maxLarg[i]);
                            testCodCli.setWidth(maxLarg[i]);
                            break;
                        case 1:
                            ragSoc.setWidth(maxLarg[i]);
                            testRagSoc.setWidth(maxLarg[i]);
                            break;
                        case 2:
                            dataDoc.setWidth(maxLarg[i]);
                            testDataDoc.setWidth(maxLarg[i]);
                            break;
                        case 3:
                            importo.setWidth(maxLarg[i]);
                            testImporto.setWidth(maxLarg[i]);
                            break;
                        default:
                            break;
                    }
                }
                i++;
            }
        }
    };

    dettagli.getViewTreeObserver().addOnDrawListener(drawListener);
    dettagli.getViewTreeObserver().removeOnDrawListener(drawListener);
}

0 个答案:

没有答案