Pregunta

I want to align my text in a way like a table, and I want it to be 3 centered text views.

Thing is, the TableLayout is a premade set up, and I don't know how many items I'm about to enter inside, so I have a ScrollView Layout before the main LinearLayout so I can pan down the view.

Even if I have table rows, I need to pad correctly the columns, so here is a picture to understand:

I attempted to work with 3 TextViews and an offset integer, here is my Java Code:

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);
        }

In the Part Name, I attempted to create a line with some formula like: offset = -length*(length*5); but it still didn't work and everything is so off map...

Is there an easier way to do this? I would like to hear suggestions, thanks!

¿Fue útil?

Solución

Fixed it:

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);

        }

enter image description here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top