将java创建的ImageView放在另一个java创建的ImageView旁边

时间:2013-03-06 00:03:19

标签: android android-layout

我正在尝试创建一项功能,允许用户点击按钮,将其带到SD卡。当用户选择图片时,它会返回带有按钮的屏幕并以ImageView显示图片。如果用户再次点击该按钮,则会将用户返回到图库。当用户此次选择图片时,它应显示在第一个ImageView的右侧。我已经仔细阅读了关于这个主题的每一个答案,并尝试了一切。不知怎的,我似乎尝试了所有的东西"完美的工作"然而它不会以某种方式对我有用。每次,我放置第一张图片,它出现在左上角,然后我放置第二张,它只是出现在第一张图片的顶部。我究竟做错了什么?请告诉我,我是愚蠢的,只需要(填空)。这是我的代码:

public class PictureSelector extends Activity {

    final int PIC_CODE = 12345;
    int picCounter = 0;
    ImageView[] imgViewArray = new ImageView[10];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_selector);
        Button bGallery = (Button) this.findViewById(R.id.bGallery);
        bGallery.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, PIC_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if (requestCode == PIC_CODE) {
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(
                            selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory
                        .decodeStream(imageStream);

                if (picCounter == 0) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                            100, 100);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
                            RelativeLayout.TRUE);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
                            RelativeLayout.TRUE);
                    imgViewArray[picCounter] = new ImageView(
                            PictureSelector.this);
                    imgViewArray[picCounter].setImageBitmap(yourSelectedImage);
                    imgViewArray[picCounter].setId(picCounter);
                    this.addContentView(imgViewArray[picCounter], layoutParams);
                    picCounter++;
                } else {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                            100, 100);
                    layoutParams.addRule(RelativeLayout.RIGHT_OF,
                            imgViewArray[picCounter - 1].getId());
                    imgViewArray[picCounter] = new ImageView(
                            PictureSelector.this);
                    imgViewArray[picCounter].setImageBitmap(yourSelectedImage);
                    imgViewArray[picCounter].setId(picCounter);
                    this.addContentView(imgViewArray[picCounter], layoutParams);
                    picCounter++;
                }
            }
        }
    }
}

以下是XML,如果相关的话:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/bGallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:text="Gallery" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

http://developer.android.com/guide/topics/ui/declaring-layout.html 在这里你必须阅读不同类型的布局