我希望以类似桌子的方式对齐文字,我希望它是3个居中的文本视图。
事实上,TableLayout是一个预制的设置,我不知道我要在里面输入多少项,所以我在主LinearLayout之前有一个ScrollView布局,所以我可以平移视图。 / p>
即使我有表行,我也需要正确填充列,所以这是一张要理解的图片:
我尝试使用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);
}
在Part Name中,我尝试使用以下公式创建一行: offset = -length *(length * 5); 但它仍然不起作用,一切都离地图......
有更简单的方法吗? 我想听听建议,谢谢!
答案 0 :(得分: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);
}