当我将textviews添加到相对布局时,在第一行的末尾,textview出错了。
如下图所示:
这是我的代码,用于显示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++;
}
}
我希望有这样的东西,有点像标签:
答案 0 :(得分:1)
希望以下代码可以帮助您:
运作:
contactWrapper是一个线性布局,我们继续将textview逐个添加到这些线性布局中并在添加之前查找contactWrapper是否有足够的空间放入下一个TextView,如果没有创建新的线性布局并且textViews加入其中。
花时间分析以下代码。
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++;
}
}
答案 1 :(得分:1)
最后,我可以使用@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) ;
}
答案 2 :(得分:0)
尝试使用 LinearLayout 和固定高度(使用dip单位)而不是WRAP_CONTENT
答案 3 :(得分:0)
尝试删除
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
因为您想要的行为是默认行为。
将Orientation horinzontal置于相对布局中。
答案 4 :(得分:0)
尝试将所需的所有文本放在一个String中,然后将所有这些文本放在一个TextView中。
最好的方法是使用StringBuilder来连接文本:
StringBuilder sb = new StringBuilder();
sb.append("str1");
sb.append("str2");
然后将字符串放在textview中 textview.setText(sb.toString());