添加图形视图android布局

时间:2016-12-19 19:39:13

标签: android android-layout

这将是我的第一个问题,所以我为可能的错误道歉。

每次按下我已添加到布局的按钮时,我都会尝试添加一个红色圆圈。我希望所有圈子都留在布局中:

<?xml version="1.0" encoding="utf-8"?>                                    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"                                    
    android:layout_height="fill_parent"                                   
    android:orientation="vertical" >                                      

    <LinearLayout                                                         
        android:id="@+id/panelJuego"                                      
        android:layout_width="match_parent"                               
        android:layout_height="match_parent"                              
        android:layout_weight="0.76"                                      
        android:orientation="horizontal" >                                

    </LinearLayout>                                                       

    <Button                                                               
        android:id="@+id/button1"                                         
        style="?android:attr/buttonStyleSmall"                            
        android:layout_width="wrap_content"                               
        android:layout_height="wrap_content"                              
        android:onClick="botonRojo"                                       
        android:text="Button" />                                          
</LinearLayout>                                                           

与我的尝试相关的java代码是:

public void botonRojo(View v) {                                               
    LinearLayout panelJuego = (LinearLayout) findViewById(R.id.panelJuego);   
    PonCirculo circulo = new PonCirculo(this, 30, 30, "#FF0000");             
    circulo.setLayoutParams(new LinearLayout.LayoutParams(                    
            LinearLayout.LayoutParams.WRAP_CONTENT,                           
            LinearLayout.LayoutParams.WRAP_CONTENT));                         
    panelJuego.addView(circulo);                                              
}                                                                             


public class PonCirculo extends View {                                        
    private int radio = 30;                                                   
    private String color;                                                     
    public PonCirculo(Context context, int x, int y, String color) {          
        super(context);                                                       
        Cx = Cx + x;                                                          
        Cy = 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);                                 
 }                                                                            

实际上每次按下按钮时,安卓屏幕上都会出现一个红色的圆圈,但是当我再次按下按钮时,会出现一个新的圆圈并且该按钮消失。谁能帮我?感谢。

1 个答案:

答案 0 :(得分:0)

我假设您只是在按下按钮时尝试将每个圆圈添加到彼此之下。

目前,您每次都会添加一个新视图,但它覆盖了上一个视图。您需要添加视图,为每个新圈子添加唯一ID。然后将新圆放在下面或您选择的任何位置。我给你一些示例代码,我更改为使用相对布局,并将每个圆放在前一个圆下。这应该让您开始寻找您想要的东西:

布局示例:

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeCircleLayout">

</RelativeLayout>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Circle"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:onClick="botonRojo"/>

</RelativeLayout>

示例代码:

public class MainActivity extends AppCompatActivity {

ArrayList<PonCirculo> viewList = new ArrayList<PonCirculo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void botonRojo(View v) {
    RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.relativeCircleLayout);
    PonCirculo circulo = new PonCirculo(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.BELOW, id);
    }
    panelJuego.addView(circulo, params);
    viewList.add(circulo);
}


public class PonCirculo extends View {
    private int radio = 30;
    private String color;
    int Cx, Cy;

    public PonCirculo(Context context, int x, int y, String color) {
        super(context);
        Cx = Cx + x;
        Cy = 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);
    }
}
}