0

我想像表格一样对齐我的文本,并且我希望它是 3 个居中的文本视图。

问题是,TableLayout 是预制的,我不知道我要在里面输入多少项目,所以我在主 LinearLayout 之前有一个 ScrollView 布局,这样我就可以向下平移视图。

即使我有表格行,我也需要正确填充列,所以这里有一张图片来理解:

我尝试使用 3 个 TextViews 和一个偏移整数,这是我的 Java 代码:

for (int i = 0; i < orderPartsNames.size(); i++) {
            // Linear row
            LinearLayout ly= new LinearLayout(getApplicationContext());
            ly.setOrientation(LinearLayout.HORIZONTAL);
            currentLy.addView(ly,8+i);

            // Part name
            TextView textName = new TextView(getApplicationContext());
            String partName = ""+orderPartsNames.get(i);
            int offset = 0;
            int length = partName.length();
            if(length >= 5)
                offset = length*8;
            else if(length == 4)
                offset = -length;
            else if(length == 3)
                offset = -length;
            else if(length == 2)
                offset = -length*20;
            else if(length == 1)
                offset = -length*25;
            textName.setText(partName);
            textName.setTextSize(20);
            textName.setPadding(10, 20, 250-offset, 20);
            ly.addView(textName);

            //System.out.println(orderPartsNames.get(i));

            // Part amount
            TextView textAmount = new TextView(getApplicationContext());
            textAmount.setText(""+orderParts.get(i).getAmount());
            textAmount.setTextSize(20);
            textAmount.setPadding(0, 20, 260, 20);
            ly.addView(textAmount);

            //System.out.println(orderParts.get(i).getAmount());

            // Part price
            TextView textPrice = new TextView(getApplicationContext());
            textPrice.setText(""+orderParts.get(i).getPricePerUnit());
            textPrice.setTextSize(20);
            textPrice.setPadding(0, 20, 0, 20);
            ly.addView(textPrice);

            //tableLayout.addView(tableRow);
        }

在零件名称中,我尝试使用以下公式创建一条线:offset = -length*(length*5); 但它仍然没有工作,一切都如此偏离地图......

有没有更简单的方法来做到这一点?想听听建议,谢谢!

4

1 回答 1

0

修复:

for (int i = 0; i < orderPartsNames.size(); i++) {
            // Linear row
            LinearLayout ly = new LinearLayout(getApplicationContext());
            ly.setOrientation(LinearLayout.HORIZONTAL);
            currentLy.addView(ly, 8 + i);

            // Part name
            TextView textName = new TextView(getApplicationContext());
            textName.setText("" + orderPartsNames.get(i));
            textName.setTextSize(20);
            LinearLayout.LayoutParams lp = new LayoutParams(
                    20, LayoutParams.WRAP_CONTENT, (float) 0.34);
            textName.setLayoutParams(lp);
            ly.addView(textName);

            // Part amount
            TextView textAmount = new TextView(getApplicationContext());
            textAmount.setText("" + orderParts.get(i).getAmount());
            textAmount.setTextSize(20);
            LinearLayout.LayoutParams lp2 = new LayoutParams(
                    20, LayoutParams.WRAP_CONTENT, (float) 0.33);
            textAmount.setLayoutParams(lp2);
            ly.addView(textAmount);


            // Part price
            TextView textPrice = new TextView(getApplicationContext());
            textPrice.setText("" + orderParts.get(i).getPricePerUnit());
            textPrice.setTextSize(20);
            LinearLayout.LayoutParams lp3 = new LayoutParams(
                    100, LayoutParams.WRAP_CONTENT);
            textPrice.setLayoutParams(lp3);
            ly.addView(textPrice);

        }

在此处输入图像描述

于 2014-01-26T10:21:59.803 回答