[英]How to use removeView()?
I was trying to create a TextView programatically and add it to my layout by using addView() function, but I get the runtime exception which is saying "The specified child already has a parent. You must call removeView() on the child's parent first". 我试图以编程方式创建一个TextView并通过使用addView()函数将其添加到布局中,但是却遇到了运行时异常,即“指定的子代已经有一个父代。必须首先在该子代的父代上调用removeView()” ”。 I've looked all the questions asked about this subject, but still couldn't understand how to and when to use removeView() function. 我已经看过所有有关此主题的问题,但仍然不明白如何以及何时使用removeView()函数。
So here's my code: 所以这是我的代码:
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int maxNumber = 6;
private Integer[] numbers = new Integer[maxNumber];
private Integer[] textSizes = new Integer[maxNumber];
private Integer[] colors = new Integer[8];
private Integer[] randomColors = new Integer[maxNumber];
private Integer[][] margins;
private LayoutParams params[];
private int maxValue = 1000;
private int maxSize = 50;
private int minSize = 15;
private int viewCount = 0;
private int startCount = 3;
private int layoutHeight;
private int layoutWidth;
private int score = 0;
private RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.layout);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
layoutHeight = displayMetrics.heightPixels;
layoutWidth = displayMetrics.widthPixels;
String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.colors);
for (int i = 0; i < colorsTxt.length; i++) {
int newColor = Color.parseColor(colorsTxt[i]);
colors[i] = newColor;
}
createTextView(3);
}
private void createTextView(int num)
{
final TextView tv = new TextView(this);
createProperties(num);
eliminateSameRandoms();
for(int i = viewCount; i < (viewCount + num); i++)
{
tv.setLayoutParams(params[i]);
tv.setText(String.valueOf(numbers[i]));
tv.setTextColor(randomColors[i]);
tv.setTextSize(textSizes[i]);
createListener(tv);
layout.addView(tv);
}
viewCount += num;
}
private void createListener(final TextView textView)
{
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Integer.parseInt(String.valueOf(textView.getText())) == getBiggest()) {
score++;
if(viewCount == startCount * startCount) {
startCount++;
createTextView(startCount);
}
else {
createTextView(startCount);
}
} else {
Intent i = new Intent(MainActivity.this, FailActivity.class);
i.putExtra("score", score);
startActivity(i);
}
}
});
}
private int getBiggest()
{
int max = 0;
for(int i : numbers)
{
if(i > max)
{
max = i;
}
}
return max;
}
private void createProperties(int num)
{
createRandoms(num);
createColors(num);
createTextSizes(num);
createMargins(num);
createParams(num);
}
private void createRandoms(int numRandoms)
{
for(int i = viewCount; i < (viewCount + numRandoms); i++)
{
numbers[i] = new Random().nextInt(maxValue);
}
}
private void createParams(int numParams)
{
params = new LayoutParams[numParams];
for(int i = viewCount; i < (viewCount + numParams); i++)
{
params[i] = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params[i].leftMargin = margins[i][0];
params[i].topMargin = margins[i][1];
}
}
private void createMargins(int numMargins)
{
margins = new Integer[numMargins][2];
for(int i = viewCount; i < (viewCount + numMargins); i++)
{
margins[i][0] = new Random().nextInt(layoutWidth - 40) + 20;
margins[i][1] = new Random().nextInt(layoutHeight - 40) + 20;
}
}
private void createTextSizes(int numTextSizes)
{
for(int i = viewCount; i < (viewCount + numTextSizes); i++)
{
textSizes[i] = new Random().nextInt(maxSize - minSize) + minSize;
}
}
private void createColors(int numColors)
{
for(int i = viewCount; i < (viewCount + numColors); i++)
{
randomColors[i] = colors[new Random().nextInt(colors.length - 1)];
}
}
private boolean inRange(int position, int other)
{
int range = 60;
return !(position - other < range && other - position < range);
}
private void eliminateSameRandoms()
{
int numRandoms = viewCount;
int i = 0;
while(i < numRandoms)
{
for(int j = 0; j < numRandoms; j++)
{
Log.d("i: " , String.valueOf(i));
Log.d("j: " , String.valueOf(j));
if(j != (numRandoms - i))
{
while (numbers[j].equals(numbers[numRandoms - (i + 1)]))
{
numbers[j] = new Random().nextInt(maxValue);
}
while (randomColors[j].equals(randomColors[numRandoms - (i + 1)]))
{
randomColors[j] = colors[new Random().nextInt(colors.length)];
}
while (textSizes[j].equals(textSizes[numRandoms - (i + 1)]))
{
textSizes[j] = new Random().nextInt(maxSize - minSize) + minSize;
}
while(inRange(margins[j][0], margins[numRandoms - (i + 1)][0]))
{
margins[j][0] = new Random().nextInt(layoutWidth - 40) + 20;
}
while(inRange(margins[j][1], margins[numRandoms - (i + 1)][1]))
{
margins[j][1] = new Random().nextInt(layoutHeight - 40) + 20;
}
}
}
i++;
}
}
}
There is only a RelativeLayout in my XML layout which has the id = layout 我的XML布局中只有一个RelativeLayout,其id为layout
I tried to use 我尝试使用
((ViewGroup)tv.getParent()).removeView(tv);
before 之前
layout.addView(tv);
at various positions in my code but none of them worked. 在我的代码中的各个位置,但没有一个起作用。
I've looked this and this but couldn't figure out the solution. 我看着这个和这个 ,但不能找出解决方案。
Where and how should I use removeView() ? 在哪里以及如何使用removeView()?
You're adding the same TextView
over and over again to your layout. 您要一遍又一遍地将相同的TextView
添加到布局中。 After the first time, the TextView
has a parent, that's why you get the error. 第一次之后, TextView
有一个父级,这就是为什么会出现错误的原因。 Initialize your TextView
in the loop and it will fix your error: 在循环中初始化TextView
,它将修复您的错误:
for(int i = viewCount; i < (viewCount + num); i++)
{
final TextView tv = new TextView(this);
tv.setLayoutParams(params[i]);
tv.setText(String.valueOf(numbers[i]));
tv.setTextColor(randomColors[i]);
tv.setTextSize(textSizes[i]);
createListener(tv);
layout.addView(tv);
}
If you initialize it like this, you create a new TextView
every time which does not have a parent yet. 如果这样初始化,则每次都创建一个没有父级的新TextView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.