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

时间:2015-05-11 10:30:31

标签: java android layout imageview

我试图动态添加Imageview 我想创建这种布局,与ImageViews重叠,为 我的android应用程序。

enter image description here

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

这是我做的代码:

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

这是我的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 个答案:

答案 0 :(得分:0)

如果您想要彼此重叠的图像(我从您的问题中理解),您应该删除此行:

 params2.addRule(RelativeLayout.BELOW,id);

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

答案 1 :(得分:0)

使用相对布局  使用此代码

<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>

答案 2 :(得分:0)

以下是如何实现它的示例。

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>

输出截图:

enter image description here

所需图片:

enter image description here enter image description here