是否可以使用两种不同的布局获得两个以上的视图?

时间:2011-12-09 12:47:02

标签: android android-view

我正在尝试保留两个自定义视图,并且我保持fadein fadeout但是我无法看到两个自定义视图只能看到一个自定义视图,如果我点击fadein我只能看到一个视图但不能同时看到两个视图。但我无法一次看到两个观点,请告诉我我错误的地方。

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用观看setVisibility方法