簡體   English   中英

動態將textview添加到relativelayout。

[英]Adding textviews dynamically to relativelayout.

當我在相對布局中添加textview時,在第一行的末尾,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++;
   }

}

我希望有這樣的東西,有點標簽實現:

在此處輸入圖片說明

希望以下代碼能對您有所幫助:

運作:

contactWrapper是一種線性布局,我們繼續將textviews逐一添加到這些線性布局中,然后再添加,如果沒有創建一個新的線性布局並將textViews添加到其中,則先確定contactWrapper是否有足夠的空間放入下一個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++;
    }
}

最后,我能夠使用@kailas的想法刪除該錯誤

我在這里發布我的方法:

public void showkeyword() {
    int counter = 0;
    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    final RelativeLayout contactWrapper =  (RelativeLayout)findViewById(R.id.key_layout);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     RelativeLayout.LayoutParams buttonparams = new RelativeLayout.LayoutParams(
          150,
          80);

     int i = 0;



    contactWrapper.removeAllViews();
    // contact wrapper is a linear Layout 
    // use LinearLayout contactWrapper = (LinearLayout) mView
    //          .findViewById(R.id.yourLinearLayout);
    int currCounter = 0;
    int currWidth = 0;
    boolean isNewLine = false;
    boolean firstLine = true;

    for(final String s : alist)
    {
            TextView textview = new TextView(this);

           RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                      RelativeLayout.LayoutParams.WRAP_CONTENT);

            rlp1.setMargins(7, 5, 7, 0);
            textview.setText(s);
            textview.setId(2000 + i);
            textview.setBackgroundColor(Color.DKGRAY);
            textview.setTextColor(Color.CYAN);
            textview.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
             contactWrapper.removeView(v);
             alist.remove(s);
                }

              });

           int width = s.length()*15;
            if((currWidth+width+150)<=screenWidth)
            {
                currWidth += width+10;
                isNewLine = false;
                currCounter++;
            }
            else{
                currWidth = width+14;
                firstLine = false ;
                isNewLine = true;
                currCounter=1;
            }



            if(i==0)
            {     rlp1.addRule(RelativeLayout.ALIGN_START);
                  textview.setLayoutParams(rlp1);
                  contactWrapper.addView(textview);
            }
            else if(isNewLine){
                 rlp1.addRule(RelativeLayout.ALIGN_LEFT);
                 rlp1.addRule(RelativeLayout.BELOW,2000-1+i );
                 textview.setLayoutParams(rlp1);
                 contactWrapper.addView(textview);
            }
            else if(firstLine)
            {
                 rlp1.addRule(RelativeLayout.RIGHT_OF,2000-1+i );
                 textview.setLayoutParams(rlp1);
                 contactWrapper.addView(textview);

            }
            else{

                 rlp1.addRule(RelativeLayout.RIGHT_OF,2000-1+i );
                 rlp1.addRule(RelativeLayout.BELOW,2000-currCounter+i );
                 textview.setLayoutParams(rlp1);
                 contactWrapper.addView(textview);

            }



            i++;
    }

     buttonparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
     buttonparams.addRule(RelativeLayout.ALIGN_BASELINE,2000-1+i);
     Button clearbtn = new Button(this);


        clearbtn.setText("clear");
       // clearbtn.setBackgroundColor(Color.RED);
       // clearbtn.setTextColor(Color.CYAN);
        clearbtn.setLayoutParams(buttonparams);
        clearbtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
         contactWrapper.removeAllViews();
         alist.clear();
            }

          });


   contactWrapper.addView(clearbtn) ;

}

嘗試使用LinearLayout固定高度 (使用浸入單元)代替WRAP_CONTENT

嘗試刪除

rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

因為您想要的行為是默認行為。

將“水平取向”放置在相對布局中。

嘗試將所有所需的文本放入一個String中,然后僅將所有這些文本放入一個TextView中。

最好的方法是使用StringBuilder合並文本:

StringBuilder sb = new StringBuilder();
sb.append("str1");
sb.append("str2");

然后將字符串放入textview textview.setText(sb.toString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM