有两种不同的布局可以获得两个以上的视图吗?

问题描述:

我想保留两个customviews,我保持淡入淡出,但我无法看到两个自定义视图能够看到只有一个自定义视图,如果我点击淡入淡出,我能够看到只有一个视图,但不是两个。但我无法一次看到两种观点,请告诉我我错在哪里。有两种不同的布局可以获得两个以上的视图吗?

MyView myview; 
MyView1 myview1; 
RelativeLayout relativeLayout; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    myview=new MyView(this); 
    myview.setBackgroundColor(color.white); 
    setContentView(myview); 
    myview = new MyView(this); 
    myview.setBackgroundColor(Color.WHITE); 

    myview1=new MyView1(this); 
    myview1.setBackgroundColor(color.white); 
    setContentView(myview1); 
    myview1 = new MyView1(this); 
    myview1.setBackgroundColor(Color.WHITE); 

    RelativeLayout relativeLayout = new RelativeLayout(this); 
    relativeLayout.setId(0); 
    relativeLayout.setBackgroundColor(Color.WHITE); 
    myview = new MyView(this); 
    myview.setId(004); 

    RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 

    myview.setLayoutParams(lp6); 
    relativeLayout.addView(myview,lp6); 
    myview1 = new MyView1(this); 
    myview1.setId(8); 

    RelativeLayout.LayoutParams lp008 = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 

    myview1.setLayoutParams(lp008); 
    relativeLayout.addView(myview1,lp008); 
    Button button1 = new Button(this); 

    RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.FILL_PARENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 

    button1.setText("click"); 
    relativeLayout.addView(button1,lp1); 
    button1.setOnClickListener(this); 
    setContentView(relativeLayout); 
} 
public class MyView extends View { 
    public MyView(Context c) { 
     super(c); 
    } 
    protected void onDraw(Canvas canvas) { 
      Bitmap _scratch = BitmapFactory.decodeResource(getResources(),R.drawable.apple); 
      canvas.drawBitmap(_scratch, 840, 450, null); 
     } 
    } 
    public class MyView1 extends View{ 
     public MyView1(Context context) { 
      super(context); 
     }  
     protected void onDraw(Canvas canvas) {                        
      Bitmap scratch=BitmapFactory.decodeResource(getResources(), R.drawable.ant);   
      canvas.drawBitmap(scratch, 840, 450, null); 
     } 
    } 
    public void onClick(View v) { 
     if(myview1.getVisibility() == View.VISIBLE) { 
      Animation out = AnimationUtils.makeOutAnimation(this, true); 
      myview1.startAnimation(out); 
      myview1.setVisibility(View.INVISIBLE); 
     } else { 
      Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); 
      myview1.startAnimation(in); 
      myview1.setVisibility(View.VISIBLE); 
     } 
    } 
} 

使用视图setVisibility方法