添加textviews动态地RelativeLayout的。 [英] Adding textviews dynamically to relativelayout.

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

问题描述

虽然我加入textviews相对布局,在第一行的末尾,TextView的是走错了。

While I am adding textviews to relative layout, at the end of first line, the textview is going wrong.

,如下面的:

.

这是我的code到diplay textviews。

here is my code to diplay textviews.

public void showkeyword()
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout fl =  (RelativeLayout)findViewById(R.id.key_layout); 
    fl.removeAllViews();
    RelativeLayout.LayoutParams params ;

        //TextView key = (TextView) inflater.inflate(R.layout.tag_keyword,null);

i = 0;

   for(String s : alist)
   {   
        TextView textview = (TextView) inflater.inflate(R.layout.tag_keyword,null);
        textview.setText(s);
        textview.setId(2000 + i);      

        if (i == 0) {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textview.setLayoutParams(rlp2);
            fl.addView(textview);

        } else {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
           // rlp2.addRule(RelativeLayout.ALIGN_BASELINE);
            rlp2.setMargins(10,0, 10,0);
            rlp2.addRule(RelativeLayout.RIGHT_OF, textview.getId() - 1);
            textview.setLayoutParams(rlp2);
            fl.addView(textview);  
        }          
       i++;
   }

}

我想有这样的事情,形式的标签实现:

I wish to have something like this, sort of a tab implementation:

推荐答案

希望下面的code会帮助你:

Hope the following code will help you out:

运作

contactWrapper是线性的布局,我们继续添加textviews到这些线性布局逐个和之前加入找到contactWrapper是否具有空间足以把在接下来的TextView,如果创建不是一个新的线性布局和textViews被添加进去了。

contactWrapper is a linear layout, we go on adding the textviews into these linear layouts one by one and before adding find whether the contactWrapper has space enough to put in the next TextView, if not a new linear layout is created and the textViews are added into it.

花时间分析如下code。

Take time analyzing the following code.

public void drawLayout() {
    int counter = 0;
    contactWrapperWidth = getResources().getDisplayMetrics().widthPixels;
    contactWrapper.setOrientation(LinearLayout.VERTICAL); 
    // contact wrapper is a linear Layout 
    // use LinearLayout contactWrapper = (LinearLayout) mView
    //          .findViewById(R.id.yourLinearLayout);
    currCounter = 0;
    currWidth = 0;
    isNewLine = false;

    row[currCounter] = new LinearLayout(getActivity());

    @SuppressWarnings("rawtypes")
    Iterator it = button.iterator();

    for (int i = 0; i < button.size(); i++) {
        it.next();
        row[currCounter].setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        currWidth += Integer
                .parseInt(button.get(i).get("width").toString());
        Log.i("Item width ", "i == "
                + button.get(i).get("width").toString());

        // contactWrapper.getw

        if (isNewLine) {
            if (currWidth < contactWrapperWidth) {
                row[currCounter]
                        .addView((View) button.get(i).get("button"));
                if (!it.hasNext()) {
                    contactWrapper.addView(row[currCounter]);
                } else {
                    if (contactWrapperWidth < (currWidth + Integer
                            .parseInt(button.get(i + 1).get("width")
                                    .toString()))) {
                        isNewLine = true;
                        contactWrapper.addView(row[currCounter]);
                        currCounter += 1;
                        row[currCounter] = new LinearLayout(getActivity());
                        currWidth = 0;
                    } else {
                        isNewLine = false;
                    }
                }
            } else {
                isNewLine = true;
                contactWrapper.addView(row[currCounter]);
                currCounter += 1;
                row[currCounter] = new LinearLayout(getActivity());
                currWidth = 0;
            }
        } else {
            if (currWidth < contactWrapperWidth) {
                if (!it.hasNext()) {

                    View view = (View) button.get(i).get("button");
                    row[currCounter].addView((View) button.get(i).get(
                            "button"));
                    contactWrapper.addView(row[currCounter]);
                } else {
                    View view = (View) button.get(i).get("button");

                    row[currCounter].addView((View) button.get(i).get(
                            "button"));
                    if (contactWrapperWidth < (currWidth + Integer
                            .parseInt(button.get(i + 1).get("width")
                                    .toString()))) {
                        isNewLine = true;
                        Logger.show(Log.INFO, "it.hasNext()",
                                "it.hasNext() contactWrapper");
                        contactWrapper.addView(row[currCounter]);
                        currCounter += 1;
                        row[currCounter] = new LinearLayout(getActivity());
                        currWidth = 0;
                    } else {
                        isNewLine = false;
                    }
                }
            } else {
                isNewLine = true;
                contactWrapper.addView(row[currCounter]);
                currCounter += 1;
                row[currCounter] = new LinearLayout(getActivity());
                currWidth = 0;
            }
        }
        counter++;
    }
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆