java - 将 TextView 动态添加到相对布局。

标签 java android android-layout layout

当我将 TextView 添加到相对布局时,在第一行末尾, TextView 出现错误。

如下图:

picture .

这是我显示 TextView 的代码。

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++;
   }

}

我希望有这样的东西,类似于选项卡实现:

enter image description here

最佳答案

希望以下代码对您有所帮助:

功能:

contactWrapper是一个线性布局,我们继续将textview逐一添加到这些线性布局中,在添加之前检查contactWrapper是否有足够的空间来放入下一个TextView,如果没有则创建一个新的线性布局并添加textView添加到其中。

花点时间分析以下代码。

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++;
    }
}

关于java - 将 TextView 动态添加到相对布局。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055289/

相关文章:

javascript - 用 Java Vert.x 客户端替换 JavaScript SockJS 客户端

org.hibernate.ejb.criteria.predicate.ComparisonPredicate.render 处的 java.lang.NullPointerException

java - systemd 找不到 Java

java - 正则表达式 Notepad++

android - 在 Android 中存储全局变量(授权 token )的最佳位置在哪里

android - 更新语句不更新字段

android应用触摸无响应

android - 非 Activity 类中的 findViewById(R.id.something)

Android 复杂表格布局

android - 将背景图像添加到 Android 中的 ListView ?