代码之家  ›  专栏  ›  技术社区  ›  Kuroiteiken

在Android系统中添加ImageButton

  •  0
  • Kuroiteiken  · 技术社区  · 7 年前

    我正在申请一个画廊。我成功地列出了我的图像,现在添加了共享按钮。我试图在图片上方显示共享按钮,而不是将用户引导到另一个表单中。对于列表和共享,我需要创建和添加动态的 imagebuttons 。但当我尝试添加共享按钮时,它们显示在错误的位置。这是我的代码:

    linearlayout ll=(linearlayout)findViewByID(r.id.llGallery);
    对于(int i=0;i<jsonarray.length();i++){
    final jsonObject jsonObject=jsonArray.getjsonObject(i);
    最终字符串photo=jsonObject.getString(“photoLink”);
    
    //照片细节进入另一个视图
    view.onclickListener cphotodetails=new view.onclickListener()。{
    
    @重写
    公共void onclick(视图V){
    意向意向=新意向(gallerydetailsactivity.this,
    GalleryPhotodetails.类);
    意向。PutExtra(“图像”,照片);
    开始活动(意图);
    }
    };
    
    view.onclickListener cshare=新视图.onclickListener()。{
    @重写
    公共void onclick(视图V){
    尝试{
    文件myfile=新文件(fotograf);
    mimetypemap mime=mimetypemap.getsingleton();
    字符串ext=myfile.getname().substring(myfile.getname().lastindexof(“.”))+1);
    字符串类型=mime.getmimetypefromextension(ext);
    intent sharingintent=新意图(“android.intent.action.send”);
    sharingingent.settype(类型);
    sharingintent.putExtra(“android.intent.extra.stream”,uri.fromfile(myfile));
    StartActivity(intent.createCheoser(sharinginent,“share with”));
    }捕获(异常E){
    //toast.makeText(getBaseContext(),e.getMessage(),toast.length_short).show();
    }
    }
    };
    
    relativelayout rel=新relativelayout(上下文);
    相对设定重力(重心垂直);
    
    imagebutton img=新建imagebutton(上下文);
    img.setclickable(真);
    img.setscaleType(imageview.scaleType.center_-inside);
    glide.with(context).load(photo).into(img);
    img.setonclicklistener(cphotodetails);
    相对添加视图(img);
    
    imagebutton share=新建imagebutton(上下文);
    share.setclickable(真);
    share.setscaleType(imageview.scaleType.fit_xy);
    glide.with(context).load(r.drawable.btn_share).into(共享);
    share.setonclicklistener(共享);
    rel.addview(共享);
    
    ll.添加视图(rel);
    }
    < /代码> 
    
    

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   tejendra singh    7 年前
    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);