0
我目前已經設置了我的代碼,用於在行佈局中顯示父佈局中的一個TextView和同一個佈局中的3個textview。我的問題是,當我想將其設置在同一行中時,3個textview顯示爲3個不同的行。我想我需要一個孩子linerlayout但這並沒有爲我工作。這3個文本視圖是動態的,因爲輸入的數據是動態的,所以我無法設置一定數量的文本視圖。這裏是我的我的代碼將如何顯示的代碼示例....動態添加父佈局並動態添加子佈局
Screenshot of 3 textviews in different rows
我的代碼如下:
//initialize linearlayout
public LinearLayout pickcontainerLayout;
int counter = 0;
//creating a new view by using functions, using keys and values from multimap
for (String key : myMultimap.keySet()) {
ArrayList<SaleOrder> sale = new ArrayList<>();
for (SaleOrder s : myMultimap.get(key)) {
sale.add(s);
}
pickcontainerLayout.addView(containerLayoutfunction(counter, sale.size(), sale, key));
View line = new View(getContext());
line.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1));
line.setBackgroundColor(Color.rgb(255,255,255));
pickcontainerLayout.addView(line);
counter++;
}
private TextView newSku(int id, String sku) {
TextView skuView = new TextView(getContext());
skuView.setId(id);
skuView.setText("SKU: " + sku);
/*LinearLayout.LayoutParams skuParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
skuView.setLayoutParams(skuParams);*/
skuView.setGravity(Gravity.LEFT);
return skuView;
}
private TextView newqty(int id, String qty) {
TextView qtyView = new TextView(getContext());
qtyView.setId(id);
qtyView.setText("QTY: " + qty);
/*LinearLayout.LayoutParams qtyParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
qtyView.setLayoutParams(qtyParams);*/
qtyView.setGravity(Gravity.CENTER);
return qtyView;
}
private TextView newlocation(int id, String location) {
TextView loc = new TextView(getContext());
loc.setId(id);
loc.setText("Location: " + location);
/*LinearLayout.LayoutParams locParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
loc.setLayoutParams(locParams);*/
loc.setGravity(Gravity.RIGHT);
return loc;
}
private LinearLayout containerLayoutfunction(int parentid, int rowId, ArrayList<SaleOrder> s, String key) {
LinearLayout layout = new LinearLayout(getContext());
layout.addView(newSale(parentid, key));
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i<rowId;i++) {
//LinearLayout layoutchild = new LinearLayout(getContext());
//layoutchild.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//layoutchild.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(newSku(rowId,s.get(i).getSku()));
layout.addView(newqty(rowId,s.get(i).getQty()));
layout.addView(newlocation(rowId,s.get(i).getlocation()));
}
return layout;
}
將其設置爲水平方向將使我的所有文本視圖都位於同一行,並且我不希望這樣,因此它必須保持垂直。除非有另外一種方式去解決它? – refugio
您可以將第一個textview留在內部線性佈局之外 – ddb