本文介绍了在Android的动态添加LinearLayouts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态添加LinearLayouts但它只是不工作。我想我只是需要另一套的眼睛看一下。谁能帮我?

 的LinearLayout parentLayout =(的LinearLayout)findViewById(R.id.parentLayout);
        lLayout =新的LinearLayout [8];
        的for(int i = 0; I< lLayout.length;我++){
            lLayout [I] =新的LinearLayout(本);
            lLayout [I] .setId(ⅰ);
            lLayout [I] .setOrientation(LinearLayout.HORIZONTAL);
            如果(ⅰ%2 == 0){
                lLayout [I] .setBackgroundColor(Color.GREEN);
            }其他{
                lLayout [I] .setBackgroundColor(Color.MAGENTA);
            }
            parentLayout.addView(lLayout [I]);
        }


解决方案

您需要设置的LayoutParams,尝试添加这样的:

  lLayout [I] .setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

I'm trying to add LinearLayouts dynamically but it's just not working. I think I just need another set of eyes to look it over. Can anyone help me?

LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
        lLayout = new LinearLayout[8];
        for(int i = 0; i < lLayout.length; i++) {
            lLayout[i] = new LinearLayout(this);
            lLayout[i].setId(i);
            lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
            if(i%2 == 0) {
                lLayout[i].setBackgroundColor(Color.GREEN);
            } else {
                lLayout[i].setBackgroundColor(Color.MAGENTA);
            }
            parentLayout.addView(lLayout[i]);
        }
解决方案

You need to set LayoutParams, try adding this:

lLayout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

这篇关于在Android的动态添加LinearLayouts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 23:28