0

我正在尝试创建一个功能,允许用户点击一个按钮,将他们带到他们的 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>
4

1 回答 1

0

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

于 2013-03-06T00:19:42.807 回答