0

テーブルのようにテキストを配置したいのですが、中央揃えの 3 つのテキスト ビューにしたいと考えています。

つまり、TableLayout は事前に作成されたセットアップであり、内部に入力しようとしている項目の数がわからないため、メインの LinearLayout の前に ScrollView Layout を配置して、ビューを下にパンできるようにします。

テーブルの行がある場合でも、列を正しくパディングする必要があるため、理解するための図を次に示します。

私は3つのTextViewとオフセット整数を操作しようとしました.ここに私の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 に答える