package com.example.paul_2.a5mai;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int first=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
start();
}
void start () {
//root layout
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
root.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
setContentView(root);
//butonul de start
Button StartBTN = new Button(this);
StartBTN.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
StartBTN.setText("Click Me");
root.addView(StartBTN);
// contine toate cele 5 layouturi
final LinearLayout linearContainer=new LinearLayout(this);
linearContainer.setOrientation(LinearLayout.HORIZONTAL);
linearContainer.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
root.addView(linearContainer);
//LINIA 1
final LinearLayout firstRow = new LinearLayout(this);
firstRow.setOrientation(LinearLayout.VERTICAL);
firstRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(firstRow);
//dimensiunea imaginii
final RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(260,260);
//params1.leftMargin =10;
//params1.topMargin = 10;
final ImageView imgView = new ImageView(MainActivity.this);
/* final ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(3) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
firstRow.addView(imgView, params1);*/
/*final ImageView imgView2 = new ImageView(this);
Random rand2 = new Random();
int rndInt2 = rand2.nextInt(3) + 1;
String imgName2 = "img" + rndInt2;
int id2 = getResources().getIdentifier(imgName2, "drawable", getPackageName());
imgView2.setImageResource(id2);
firstRow.addView(imgView2, params1);
final ImageView imgView3 = new ImageView(this);
Random rand3 = new Random();
int rndInt3 = rand3.nextInt(3) + 1;
String imgName3 = "img" + rndInt3;
int id3 = getResources().getIdentifier(imgName3, "drawable", getPackageName());
imgView3.setImageResource(id3);
firstRow.addView(imgView3, params1);*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout secondRow = new LinearLayout(this);
secondRow.setOrientation(LinearLayout.VERTICAL);
secondRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(secondRow);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout thirdRow = new LinearLayout(this);
thirdRow.setOrientation(LinearLayout.VERTICAL);
thirdRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(thirdRow);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout fourthRow = new LinearLayout(this);
fourthRow.setOrientation(LinearLayout.VERTICAL);
fourthRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(fourthRow);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final LinearLayout fivethRow = new LinearLayout(this);
fivethRow.setOrientation(LinearLayout.VERTICAL);
fivethRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
linearContainer.addView(fivethRow);
StartBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
first=1;
//ImageView imgView = new ImageView(MainActivity.this);
Random rand = new Random();
int rndInt = rand.nextInt(3) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
firstRow.addView(imgView, params1);
ImageView imgView2 = new ImageView(MainActivity.this);
Random rand2 = new Random();
int rndInt2 = rand2.nextInt(3) + 1;
String imgName2 = "img" + rndInt2;
int id2 = getResources().getIdentifier(imgName2, "drawable", getPackageName());
imgView2.setImageResource(id2);
firstRow.addView(imgView2, params1);
ImageView imgView3 = new ImageView(MainActivity.this);
Random rand3 = new Random();
int rndInt3 = rand3.nextInt(3) + 1;
String imgName3 = "img" + rndInt3;
int id3 = getResources().getIdentifier(imgName3, "drawable", getPackageName());
imgView3.setImageResource(id3);
firstRow.addView(imgView3, params1);
final Animation animSlide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide);
firstRow.startAnimation(animSlide);
}
});
}
}
我想通过单击StartBTN随机生成此图像。代码可以工作,但问题是我第一次点击按钮时生成的图像仍然存在,下次我点击按钮时它们不会改变。我每次点击按钮都需要生成。我能做什么?我应该在哪里声明对象?我怎么能这样做?提前谢谢!
将对象声明移出onsite事件后的logcat: 05-17 13:49:26.236 8757-8757 / com.example.paul_2.a5mai E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.example.paul_2.a5mai,PID:8757 java.lang.IllegalStateException:指定的子级已有父级。您必须先在孩子的父母身上调用removeView() 在android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
在android.view.ViewGroup.addView(ViewGroup.java:4258)
在android.view.ViewGroup.addView(ViewGroup.java:4230)
在com.example.paul_2.a5mai.MainActivity $ 1.onClick(MainActivity.java:127)
在android.view.View.performClick(View.java:5637)
在android.view.View $ PerformClick.run(View.java:22429)
在android.os.Handler.handleCallback(Handler.java:751)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
你认为你每次都得到相同的号码,但你没有。您要将所有内容添加到第一行,但只能看到第一行顶部显示的图像。在按钮侦听器中创建ImageView的末尾,将ImageView视图添加到第二行和第三行,您将生成包含随机图像的新行:
StartBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView imgView = new ImageView(MainActivity.this);
Random rand = new Random();
int rndInt = rand.nextInt(3) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
firstRow.addView(imgView, params1);
ImageView imgView2 = new ImageView(MainActivity.this);
Random rand2 = new Random();
int rndInt2 = rand2.nextInt(3) + 1;
String imgName2 = "img" + rndInt2;
int id2 = getResources().getIdentifier(imgName2, "drawable", getPackageName());
imgView2.setImageResource(id2);
secondRow.addView(imgView2, params1);
ImageView imgView3 = new ImageView(MainActivity.this);
Random rand3 = new Random();
int rndInt3 = rand3.nextInt(3) + 1;
String imgName3 = "img" + rndInt3;
int id3 = getResources().getIdentifier(imgName3, "drawable", getPackageName());
imgView3.setImageResource(id3);
thirdRow.addView(imgView3, params1);
final Animation animSlide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide);
firstRow.startAnimation(animSlide);
secondRow.startAnimation(animSlide);
thirdRow.startAnimation(animSlide);
}
});
在添加新视图之前删除视图
即:
firstRow.removeAllViews();
firstRow.addView(imgView, params1);
或
firstRow.removeView(v);
firstRow.addView(imgView, params1);
或
firstRow.removeAllViewsInLayout();
firstRow.addView(imgView, params1);
答案 1 :(得分:0)
每次都不要添加ImageView。问题是,第一次添加的图像没有被删除。所以它仍然显示出来。
只需在按钮点击之外执行以下操作。
ImageView imgView = new ImageView(MainActivity.this);
ImageView imgView2 = new ImageView(MainActivity.this);
ImageView imgView3 = new ImageView(MainActivity.this);
现在在您的点击事件中,获取图片视图并更改此类资源
imgView.setImageResource(R.drawable.bike);
不要一次又一次地添加视图。这些陈述只能在您的代码中完成一次
thirdRow.addView(imgView3, params1);