如何创建图像视图重叠的布局?

时间:2022-11-19 09:09:41

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

我正在尝试动态添加Imageviews,我想为我的Android应用程序创建这种布局,ImageViews重叠。

如何创建图像视图重叠的布局?

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

我不知道如何在java代码中设置重叠。我想使用java代码因为我正在动态添加Imageview!

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:

这是我的java代码:

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

3 个解决方案

#1


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

如果您想要其他内容,请随时在此答案下发表评论

#2


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>

#3


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:

如何创建图像视图重叠的布局?如何创建图像视图重叠的布局?

#1


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

如果您想要其他内容,请随时在此答案下发表评论

#2


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>

#3


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:

如何创建图像视图重叠的布局?如何创建图像视图重叠的布局?