在Shy Ward的宝贵帮助下,我能够为我的布局添加视图。现在,当按下第二个删除按钮(“quitar”)时,我正在尝试删除它们。 我已经尝试了以下代码,但它对我不起作用:
public void quitar(View v) {
int id = 0;
if(!viewList.isEmpty()) {
id = viewList.get(viewList.size()-1).getId();
viewList.remove(id);
}
}
然后我认为在从列表中删除视图之前,我应该“实际删除它”。我试过这个没有好结果:
public void quitar(View v) {
int id = 0;
RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.panelJuego);
Circulo circulo = new Circulo(this, 30, 30, "#000000");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
if(!viewList.isEmpty()) {
id = viewList.get(viewList.size()-1).getId();
params.addRule(id);
panelJuego.addView(circulo, params);
viewList.remove(id);
}
}
从我读过的内容中我感觉可能需要使用ArrayAdapter,但我不知道该怎么做。 这将是我尝试使用ArrayAdapter但它也无法正常工作:
public class MainActivity extends Activity {
ArrayList<Circulo> viewList = new ArrayList<Circulo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void poner(View v) {
ArrayAdapter<Circulo> adapter = new ArrayAdapter(this, R.id.panelJuego, viewList);
RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.panelJuego);
Circulo circulo = new Circulo(this, 30, 30, "#FF0000");
circulo.setId(View.generateViewId());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
if(!viewList.isEmpty()) {
int id = viewList.get(viewList.size()-1).getId();
params.addRule(RelativeLayout.RIGHT_OF, id);
}
panelJuego.addView(circulo, params);
viewList.add(circulo);
adapter.add(circulo);
adapter.notifyDataSetChanged();
}
public void quitar(View v) {
int id = 0;
ArrayAdapter<Circulo> adapter = new ArrayAdapter(this, R.id.panelJuego, viewList);
if(!viewList.isEmpty()) {
adapter.remove(adapter.getItem(viewList.size()-1));
adapter.notifyDataSetChanged();;
}
}
public class Circulo extends View {
private int radio = 30;
private String color;
int Cx, Cy;
public Circulo(Context context, int x, int y, String color) {
super(context);
Cx = x;
Cy = y;
this.color = color;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor(color));
canvas.drawCircle(Cx, Cy, radio, paint);
}
}
}
布局主要是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/panelJuego"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.76"
android:orientation="horizontal" >
</RelativeLayout>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="quitar"
android:text="Quitar" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/button2"
android:onClick="poner"
android:text="Poner" />
</RelativeLayout>
我希望这有助于更好地理解我正在尝试做的事情。
答案 0 :(得分:0)
我终于可以得到我想要做的事情,添加红色圆圈并使用两个按钮删除最后一个。代码是:
public void poner(View v) {
RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.panelJuego);
Circulo circulo = new Circulo(this, 30, 30, "#FF0000");
circulo.setId(View.generateViewId());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 80);
if(!viewList.isEmpty()) {
int id = viewList.get(viewList.size()-1).getId();
params.addRule(RelativeLayout.RIGHT_OF, id);
}
panelJuego.addView(circulo, params);
viewList.add(circulo);
}
public void quitar(View v) {
int id = 0;
if(!viewList.isEmpty()) {
id = viewList.size() - 1;
viewList.get(id).setVisibility(View.INVISIBLE);
viewList.remove(id);
}
}
public class Circulo extends View {
private int radio = 30;
private String color;
int Cx, Cy;
public Circulo(Context context, int x, int y, String color) {
super(context);
Cx = x;
Cy = y;
this.color = color;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor(color));
canvas.drawCircle(Cx, Cy, radio, paint);
}
}