以编程方式创建的linearlayout不会包含内容

时间:2016-01-12 09:04:24

标签: android android-linearlayout

我正在以编程方式创建linearlayout,然后我正在添加视图。但addview函数只添加第一行项(第二行for循环)。我该如何解决这个问题。我试图将LinearLayout.LayoutParams.WRAP_CONTENT更改为5000px,但那仍然只显示第一项。当我查看日志时,for循环运行良好。

getChildCount方法在for循环中返回true值:

Crashlytics.log(Log.ASSERT, shoppingList.Title + "için: ", linearLayout.getChildCount() + "");

此外,我尝试在for循环后调用invalidaterequestlayout方法,但那仍然不起作用。

for(ShoppingList shoppingList : shoppingLists){

        LinearLayout linearLayout = new LinearLayout(MainActivity.activity);
        containerLL.addView(linearLayout);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(14, 7, 14, 7);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setBackgroundResource(R.drawable.rounded_gray);



        for(int i = 0 ; i < shoppingList.shopLists.size() ; i ++){

            Crashlytics.log(Log.ASSERT, shoppingList.Title + "için: ", linearLayout.getChildCount() + "");
            ShopList shopList = shoppingList.shopLists.get(i);

            View v = MainActivity.inflater.inflate(R.layout.row_list, linearLayout, false);

            TextView        listTitle          = (TextView)        v.findViewById(R.id.listTitle);
            TextView        brandTV            = (TextView)        v.findViewById(R.id.brandTV);
            TextView        descriptionTV      = (TextView)        v.findViewById(R.id.descriptionTV);
            TextView        sizeTV             = (TextView)        v.findViewById(R.id.sizeTV);
            LinearLayout    removeLL           = (LinearLayout)    v.findViewById(R.id.removeLL);
            FrameLayout     seperatorFL        = (FrameLayout)     v.findViewById(R.id.seperatorFL);

            listTitle.setVisibility(i == 0 ? View.VISIBLE : View.GONE);
            listTitle.setText(shoppingList.Title);
            descriptionTV.setText(shopList.description);
            brandTV.setText(shopList.brandName);
            sizeTV.setText(shopList.description);

            removeLL.setVisibility(i == shoppingList.shopLists.size() - 1 ? View.VISIBLE : View.GONE);
            seperatorFL.setVisibility(i == shoppingList.shopLists.size() - 1 ? View.GONE : View.VISIBLE);

            Crashlytics.log(Log.ASSERT, "width : " + v.getWidth() + " " + "height" + v.getHeight(), v.getX() + " X " + " " + v.getY() + " Y ");
            linearLayout.addView(v);

        }
    }

2 个答案:

答案 0 :(得分:1)

尝试添加

linearLayout.setOrientation(LinearLayout.VERTICAL);

答案 1 :(得分:0)

尝试将containerLL.addView(linearLayout);行移到第二个for循环之后。

for(ShoppingList shoppingList : shoppingLists){

    LinearLayout linearLayout = new LinearLayout(MainActivity.activity);
    .
    .
    .
    for(.....){
        .
        .
        .
    }

    containerLL.addView(linearlayout);
}