简体   繁体   中英

How can I create a layout with imageviews overlapping?

I'm trying to add Imageviews dynamically and I would like to create this kind of layout, with ImageViews overlapping, for my android application.

在此处输入图片说明

I don't know how to set the overlapping in java code. I want to use java code becouse I'm adding Imageview dynamically!

this is the code that I did:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/frame1">
    </RelativeLayout>

and this is my java code:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    mImageView = new ImageView (this);
    mImageView.setId(1);
    name = "0" + ".jpg";
    Bitmap bitmap = BitmapFactory.decodeFile(url_image+name);
    params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
    params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,1);
    params1.addRule(RelativeLayout.ALIGN_PARENT_START,1);
    params1.addRule(RelativeLayout.ALIGN_TOP,1);
    mImageView.setImageBitmap(bitmap);
    layout.addView(mImageView,params1);
        for (int i = 1;i < num_max; i++){
            mImageView = new ImageView (this);
            mImageView.setId(i+1);
            name = String.valueOf(i) + ".jpg";
            int id = mImageView.getId() - 1 ;
            bitmap = BitmapFactory.decodeFile(url_image+name);
            mImageView.setImageBitmap(bitmap);
            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            params2.addRule(RelativeLayout.BELOW,id);
           params2.addRule(RelativeLayout.ALIGN_START,id);
            params2.addRule(RelativeLayout.ALIGN_LEFT,id);
            layout.addView(mImageView,params2);
    }

If you want to images overlapping each other(as i understood from your question) you should remove this line :

 params2.addRule(RelativeLayout.BELOW,id);

If you wanted something else feel free to post comment below this answer

Use relative layout for it use this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/iv_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/iv"
     />
</RelativeLayout>

Following is the example of how to achieve it.

MainActivity.Java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            RelativeLayout layout = (RelativeLayout)               findViewById(R.id.frame1);


            //Add first image view
            ImageView mImageView = new ImageView (this);
            mImageView.setId(1);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.color1);
            RelativeLayout.LayoutParams params1 = new       RelativeLayout.LayoutParams(300, 300);
            mImageView.setImageBitmap(bitmap);
            layout.addView(mImageView,params1);


            //Add second image view
            ImageView mImageView2 = new ImageView (this);
            mImageView2.setId(2);
            Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.color2);
            mImageView2.setImageBitmap(bitmap2);
            RelativeLayout.LayoutParams params2 = new       RelativeLayout.LayoutParams(300,300);
            params2.setMargins(150, 150, 0, 0);
            layout.addView(mImageView2,params2);

        }
    }

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sample.MainActivity" >

     <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/frame1">
    </RelativeLayout>

</RelativeLayout>

Output Screenshot:

在此处输入图片说明

Required Images:

在此处输入图片说明在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM