如何在特定的相对布局上布置绘图对象?

时间:2015-01-18 07:07:16

标签: android android-layout imageview relativelayout android-drawable

我有一个FrameLayout,我为它添加了一个RelativeLayout。我的相对布局中有一些按钮。我可以使用以下代码更改与relativeLayout相关的位置:

ibrllp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ibrllp.topMargin=0;
ibrllp.leftMargin=0;
ib = new ImageButton(this);
ib.setImageResource(R.drawable.image);
ib.setLayoutParams(ibrllp);
rl.addView(ib);

我想添加一个imageView并在此relativeLayout上绘制一些线条。使用此代码:

rl= (RelativeLayout) findViewById(R.id.relativeLayout2);
fllp =new FrameLayout.LayoutParams(600,400);
fllp.gravity=Gravity.CENTER;
rl.setLayoutParams(fllp);

ibllpImageView= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ibllpImageView.topMargin=100;
ibllpImageView.leftMargin=100;

drawingImageView = new ImageView(this);
bitmap = Bitmap.createBitmap((int) getWindowManager()
  .getDefaultDisplay().getWidth(), (int) getWindowManager()
  .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
paint = new Paint();
paint.setStrokeWidth(10);
paint.setColor(Color.RED);
drawingImageView.setLayoutParams(ibllpImageView);

rl.addView(drawingImageView); 
canvas.drawLine(0, 0 , 400, 400, paint);      

它画了一条线但处于错误的位置。我想能够设置与ralativeLayout相关的行的起点坐标。就像纽扣......但我不能...... 这段代码是我的Xml:

   <FrameLayout
    android:id="@+id/frameLayout4Chob"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/purple"
    android:layout_weight="0.43" >
    <RelativeLayout
        android:id="@+id/relativeLayout4Chob"
        android:layout_width="match_parent"
        android:layout_height="414dp"
        android:layout_gravity="center"
        android:background="@drawable/blue" >         
    </RelativeLayout>
  </FrameLayout>

问题出在哪里?

0 个答案:

没有答案