在Android中将ImageButton添加到另一个顶部

时间:2018-09-04 06:59:38

标签: android imagebutton

我正在应用程序中创建图库。我成功列出了图像,现在添加了它的共享按钮。我试图将用户的共享按钮显示在图像上方,而不是将用户引导到另一种形式。为了列出和共享,我需要创建并添加动态ImageButtons。但是,当我尝试添加共享按钮时,它们显示在错误的位置。这是我的代码:

LinearLayout ll = (LinearLayout) findViewById(R.id.llGallery);
for (int i = 0; i < jsonArray.length(); i++) {
    final JSONObject jsonObject = jsonArray.getJSONObject(i);
    final String Photo= jsonObject.getString("PhotoLink");

    // PhotoDetails into another view
    View.OnClickListener cPhotoDetails= new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(GalleryDetailsActivity.this,
                    GalleryPhotoDetails.class);
            intent.putExtra("Image", Photo);
            startActivity(intent);
        }
    };

    View.OnClickListener cShare= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                File myFile = new File(fotograf);
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
                String type = mime.getMimeTypeFromExtension(ext);
                Intent sharingIntent = new Intent("android.intent.action.SEND");
                sharingIntent.setType(type);
                sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
                startActivity(Intent.createChooser(sharingIntent, "Share with"));
            } catch (Exception e) {
                //  Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    };

    RelativeLayout rel = new RelativeLayout(context);
    rel.setGravity(Gravity.CENTER_VERTICAL);

    ImageButton img = new ImageButton(context);
    img.setClickable(true);
    img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    Glide.with(context).load(Photo).into(img);
    img.setOnClickListener(cPhotoDetails);
    rel.addView(img);

    ImageButton share = new ImageButton(context);
    share.setClickable(true);
    share.setScaleType(ImageView.ScaleType.FIT_XY);
    Glide.with(context).load(R.drawable.btn_share).into(share);
    share.setOnClickListener(cShare);
    rel.addView(share);

    ll.addView(rel);
}

Gallery

1 个答案:

答案 0 :(得分:0)

RelativeLayout rel = new RelativeLayout(context);
        rel.setGravity(Gravity.CENTER_VERTICAL);

        ImageView img = new ImageView(context);
        img.setClickable(true);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(context).load(Photo).into(img);
        img.setOnClickListener(cPhotoDetails);
        rel.addView(img);

        ImageButton share = new ImageButton(context);
        share.setClickable(true);
        share.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(context).load(R.drawable.btn_share).into(share);
        share.setOnClickListener(cShare);
        rel.addView(share);

        ll.addView(rel);