如何将一个imageview放在另一个上?

时间:2014-02-10 10:39:00

标签: android android-layout android-imageview

我想将一个imageview放在另一个上。我要放在上面的imageview位于下方。

这是我的代码:

for (int i = 1; i <= numberofuploads; i++) {
        if (i == 1) {
            relativeframe1 = new RelativeLayout(this);
            RelativeLayout.LayoutParams paramslayout1 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT / 2,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
            paramslayout1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            relativeframe1.setLayoutParams(paramslayout1);
            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
                    pxrparams1a, pxrparams1b);

            imgvwphoto1 = new ImageView(this);
            imgvwphoto1.setId(1);
            imgvwphoto1.setAlpha(500);
            imgvwphoto1.setBackgroundResource(R.drawable.plusicon3);
            // params1.setMargins(0, 90, 100, 40);

            params1.setMargins(left1, top1, right1, bottom1);
            imgvwphoto1.setLayoutParams(params1);               
            relativeframe1.addView(imgvwphoto1);
            imgvwphoto1.setScaleType(ScaleType.MATRIX);
            imgvwphoto1.setOnTouchListener(this);               
            RelativeLayout.LayoutParams paramscomponent1 = new RelativeLayout.LayoutParams(
                    irpc1, RelativeLayout.LayoutParams.MATCH_PARENT);               
            paramscomponent1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

imgvwcomponent1 = new ImageView(this);
imgvwcomponent1.setAlpha(0);
imgvwcomponent1                     .setBackgroundResource(R.drawable.leftpartdoubleheartframe);
imgvwcomponent1.setBackgroundResource(componentname.getImagename());
imgvwcomponent1.setLayoutParams(paramscomponent1);

            relativeframe1.addView(imgvwcomponent1);

            relativeframe.addView(relativeframe1);

        } else {
            relativeframe2 = new RelativeLayout(this);
            RelativeLayout.LayoutParams paramslayout2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT / 2,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
            paramslayout2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            relativeframe1.setLayoutParams(paramslayout2);
            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                    pxrparams2a, pxrparams2b);// 106.67dp, 120dp
            imgvwphoto2 = new ImageView(this);
            imgvwphoto2.setId(2);
            imgvwphoto2.setAlpha(500);
            imgvwphoto2.setBackgroundResource(R.drawable.plusicon3);

            // params2.setMargins(165, 90, 0, 40);
            params2.setMargins(left2, top2, right2, bottom2);
            imgvwphoto2.setLayoutParams(params2);
            relativeframe2.addView(imgvwphoto2);
            imgvwphoto2.setScaleType(ScaleType.MATRIX);

            imgvwphoto2.setOnTouchListener(this);
            RelativeLayout.LayoutParams paramscomponent2 = new RelativeLayout.LayoutParams(
                    irpc2, RelativeLayout.LayoutParams.MATCH_PARENT);// 103.33dp

            paramscomponent2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

            imgvwcomponent2 = new ImageView(this);
            imgvwcomponent2.setAlpha(500);
            imgvwcomponent2
                    .setBackgroundResource(R.drawable.rightpartdoubleheartframe);
            imgvwcomponent2.setLayoutParams(paramscomponent2);

            relativeframe2.addView(imgvwcomponent2);

            relativeframe.addView(relativeframe2);

        }
    }

点击加号图标,图片上传到imgvwphoto,与心框重叠,我只想在心框内查看。不在心框之外。

1 个答案:

答案 0 :(得分:0)

您应该使用两个单独的布局,并使用<include>语句将其放在另一个布局中

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

    <include layout="@layout/lay1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>


    <include layout="@layout/lay2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</merge>