【问题标题】:How to align/pad text in Java Android如何在 Java Android 中对齐/填充文本
【发布时间】:2017-07-31 20:37:28
【问题描述】:

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

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

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

我尝试使用 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);
        }

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

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

【问题讨论】:

    标签: java textview alignment android-tablelayout pad


    【解决方案1】:

    已修复:

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

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2020-09-06
      • 1970-01-01
      • 2016-01-28
      相关资源
      最近更新 更多