我自定义了一个继承View的控件。重载了OnDraw 画了点东西然后我想用lineairLayout或者relativelayout什么的把它和另外一些默认的比如Button,ImageView放在一起,
但是如果像下面这样的方法却无法得到想要的结果,好像2个控件都在同一个地方 而不是想要的Button在MyView的下面
到底是怎么回事呢? 就算用LineairLayout也是一样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    > <com.entrainement.AndroidAnimation.MyView  
      android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
      android:id="@+id/imgView"
     />
<Button
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonNext"
android:text="To camera test"
android:layout_below="@id/imgView"
/>
</RelativeLayout>

解决方案 »

  1.   

    这个布局文件ms没错,把com.entrainement.AndroidAnimation.MyView替换成自带的View显示正常,看看是不是定义的View哪里错了。
      

  2.   

    是这样的。 
    public class MyView extends View{
    Bitmap bitmap;
    Paint paint;
    public MyView(Context context) {
    super(context);
    paint = new Paint();
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    // TODO Auto-generated constructor stub
    }
    public MyView(Context context, AttributeSet attrs) {
    super(context,attrs);
    paint = new Paint();
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    // TODO Auto-generated constructor stub
    }
    @Override
    protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);

    paint.setTextSize(15);
    canvas.drawBitmap(bitmap,10,10,paint);

    }
    }
    如果和其他控件一起放在Layout里无法像普通的预定义的控件一样能按着顺序自己安排
      

  3.   


    查了一下自定义的View还要重写 onMeasure计算View的合适高度和宽度,加上看一下 @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(bitmap.getWidth(), bitmap.getHeight()); 
      }