动态添加父布局,动态添加子linearlayout

时间:2016-07-05 14:44:57

标签: android android-layout android-fragments

我目前的代码设置为在父布局中显示一个TextView,在同一个布局中显示3个textview。我的问题是,当我想在一组中设置同一行时,3个文本视图显示为3个不同的行。我想我需要一个儿童衬垫布局,但这对我没有用。 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;
}

1 个答案:

答案 0 :(得分:0)

你应该将三个TextViews包含在一个水平方向的LinearLayout中,这样textview将自动在同一行上。容器将水平FILL_PARENT作为LayuoutParam,而textview应该有WRAP_CONTENT,重力分配给LEFT(第一个),CENTER(第二个)和RIGHT(第三个)。