android.widget.ImageButton.setId()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(108)

本文整理了Java中android.widget.ImageButton.setId()方法的一些代码示例,展示了ImageButton.setId()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ImageButton.setId()方法的具体详情如下:
包路径:android.widget.ImageButton
类名称:ImageButton
方法名:setId

ImageButton.setId介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

View linearLayout =  findViewById(R.id.your_main_xml);
     ImageButton newButton=new ImageButton(this);
     newButton.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.your_image)); 
     newButton.setId(5);
     noLisnText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
     noLisnText.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
     ((LinearLayout) linearLayout).addView(newButton);

代码示例来源:origin: stackoverflow.com

ImageButton mImageButton = new ImageButton(this);
mImageButton.setId(View.generateViewId());

代码示例来源:origin: stackoverflow.com

ImageButton mImageButton = new ImageButton(this);
mImageButton.setId(R.id.imageButtonId1);

代码示例来源:origin: stackoverflow.com

ImageButton ib = new ImageButton(this);
ib.setId(i);

代码示例来源:origin: stackoverflow.com

public View getView(final int position, View convertView, ViewGroup parent){

  View MyView;
  ImageButton imageFolders;

  if(convertView==null){
    LayoutInflater li = ((Activity) MyContext).getLayoutInflater();
    MyView = li.inflate(R.layout.gallery_adapter, null);
   }else{
    MyView = convertView;
   }

   imageFolders = (ImageButton) MyView.findViewById(R.id.folder);

   try {
    imageFolders.setImageBitmap(bMap);
    imageFolders.setOnClickListener(this);
    imageFolders.setId(products.get(position).getId());
   } catch (Exception e) {}

   MyView.setLayoutParams(70, 70);
   return MyView;
}

代码示例来源:origin: stackoverflow.com

public View getView(final int position, View convertView, ViewGroup parent) {

  View MyView;

  if(convertView==null) 
  {
    // Inflate the layout
    LayoutInflater li = ((Activity) MyContext).getLayoutInflater();

    MyView = li.inflate(R.layout.gallery_adapter, null);
    ImageButton imageFolders = (ImageButton) MyView.findViewById(R.id.folder);

    try {

      imageFolders.setImageBitmap(bMap);
      imageFolders.setOnClickListener(this);
      imageFolders.setId(products.get(position).getId());
    } catch (Exception e) {

    }

    MyView.setLayoutParams(70, 70);
  }
  else
    MyView = convertView;
  return MyView;
}

代码示例来源:origin: stackoverflow.com

for (int i =0;i<adapt_objmenu.image_array.length;i++){
   ImageButton b1 = new ImageButton(myrefmenu);
    b1.setId(100 + i);
     b1.setImageResource(R.drawable.imagename);
    // b1.setText(adapt_objmenu.city_name_array[i]);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    if (i > 0) {
      lp.addRule(RelativeLayout.RIGHT_OF, b1.getId() - 1);
    }   
    b1.setLayoutParams(lp);
    relative.addView(b1);

//relate.addView(b1, i,  new RelativeLayout.LayoutParams(width,height));

//height = height+80;


}

代码示例来源:origin: stackoverflow.com

for (int i=0; i<numRows; i++) {
  /*
   * 1. Make the row and attach it to myTable. For some reason this doesn't seem
   * to return the TableRow as you might expect from the xml, so you need to
   * receive the View it returns and then find the TableRow and other items, as
   * per step 2.
   */
  LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View v =  inflater.inflate(R.layout.my_table_row, myTable, true);

  // 2. Get all the things that we need to refer to to alter in any way.
  TableRow    tr        = (TableRow)    v.findViewById(R.id.profileTableRow);
  ImageButton rowButton = (ImageButton) v.findViewById(R.id.rowButton);
  TextView    rowText   = (TextView)    v.findViewById(R.id.rowText);

  // 3. Configure them out as you need to
  rowText.setText("Text for this row");
  rowButton.setId(i); // So that when it is clicked we know which one has been clicked!
  rowButton.setOnClickListener(this); // See note below ...           

  /*
   * To ensure that when finding views by id on the next time round this
   * loop (or later) gie lots of spurious, unique, ids.
   */
  rowText.setId(1000+i);
  tr.setId(3000+i);
}

代码示例来源:origin: stackoverflow.com

setListAdapter(new ArrayAdapter<Object>(this, R.layout.row, R.id.Browse_DateTime, ourRows) {
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
     View v = convertView;
     if (v == null) {
       LayoutInflater vi = (LayoutInflater)BrowseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       v = vi.inflate(R.layout.row, null);  
     }    
     TextView time = (TextView) v.findViewById (R.id.Browse_DateTime);
     time.setText(ourRows[position].dateTime);
     ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
     delete.setFocusable(false);
     delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));  
     delete.setOnClickListener(BrowseActivity.this);
     delete.setId(position);
     return v;
   }
 };)

代码示例来源:origin: stackoverflow.com

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);
ImageButton b = new ImageButton(this);
b.setText("ImageButton");
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b.setId(MY_BUTTON);
i1Btn = R.drawable.image_name;
i1.setImageResource(i1Btn);
b.setOnClickListener(this);
ll.addView(b);

代码示例来源:origin: stackoverflow.com

for (int i = 0; i < 50; i++) {
  ImageButton b1 = new ImageButton(myrefmenu);
  b1.setId(100 + i);
  b1.setImageResource(R.drawable.imagename);
  // b1.setText(adapt_objmenu.city_name_array[i]);
  RelativeLayout.LayoutParams lp = new    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  RelativeLayout.LayoutParams.WRAP_CONTENT);
  if (i > 0) {
    lp.addRule(RelativeLayout.RIGHT_OF, b1.getId() - 1);
  }   
   b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {

      Toast.makeText(AllbuttonimagesForSelectionActivity.this,
          "ImageButton is clicked!", Toast.LENGTH_SHORT)
          .show();
    }
  });
  b1.setLayoutParams(lp);
  relative.addView(b1);
}

代码示例来源:origin: stackoverflow.com

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
   for (int j = 0; j < 4; j++) {
     ImageButton myImageButton = new ImageButton(this); //generate ImageButton
     myImageButton.setId(j); //Set Id of button
     //Generate Random Number to place ImageButtons in random position
     int min = 40;
     int max = 60;
     Random r = new Random();
     int randomNum = r.nextInt(max - min + 1) + min;
     myImageButton.setBackgroundResource(R.drawable.chatbox); //Set background of ImageButton
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
     params.leftMargin = randomNum+j*randomNum; //Generate left margin dynamically
     params.topMargin = randomNum +j*randomNum; //Generate right margin dynamically
     relativeLayout.addView(myImageButton, params); //Add view
     myImageButton.setOnClickListener(getButtonAndDoAction(myImageButton)); //Add OnClickListener
   }

代码示例来源:origin: stackoverflow.com

RelativeLayout parent = (RelativeLayout)findViewById(R.id.parent);
   ImageButton ib = new ImageButton(getApplicationContext());
   ib.setId(1);
   ib.setBackgroundResource(R.drawable.cross_button);
   RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
   ib.setLayoutParams(lp);
   parent.addView(ib);
   ImageView iv = new ImageView(getApplicationContext()); 
   iv.setBackgroundResource(R.drawable.myshape);
   iv.setImageResource(R.drawable.ic_launcher);
   RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(50,50);
   lp1.addRule(RelativeLayout.RIGHT_OF, ib.getId());
   lp1.addRule(RelativeLayout.BELOW, ib.getId());
   iv.setLayoutParams(lp1);
   parent.addView(iv);

代码示例来源:origin: stackoverflow.com

final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);

RelativeLayout rl = new RelativeLayout(this);

ImageView cb1 = new ImageView(this);
cb1.setId(1);
cb1.setImageResource(R.drawable.ic_launcher);

ImageButton cb2 = new ImageButton(this);
cb2.setImageResource(R.drawable.ic_launcher);
cb2.setBackground(null);
cb2.setId(2);

LayoutParams rp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

rp.addRule(RelativeLayout.BELOW, cb1.getId());

cb2.setLayoutParams(rp);

rl.addView(cb1);
rl.addView(cb2);

lm.addView(rl);

代码示例来源:origin: stackoverflow.com

RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent);
 LinearLayout container = new LinearLayout(this);
 for (int i = 0; i < 5; i++) {
   LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   ImageButton ib = new ImageButton(this);
   ib.setId(i);
   ib.setLayoutParams(layoutParams);
   ib.setBackgroundResource(R.drawable.ic_launcher);
   container.addView(ib);
   if (ib.getId() == position) {
     ib.setBackgroundResource(R.drawable.black_circle_32x32);
   }
 }
 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,
     RelativeLayout.TRUE);
 rlLayout.addView(container, layoutParams);

代码示例来源:origin: stackoverflow.com

for(int but=0; but<ClueList.size(); but++){
  ImageButton clueBut = new ImageButton(this);
   clueBut.setBackgroundResource(0);
   clueBut.setImageBitmap(ClueList.get(but).btmp);
   clueBut.setId(but);
   //clueBut.setOnClickListener(this);

   tr0.addView(clueBut);
}

代码示例来源:origin: stackoverflow.com

imageButton.setBackgroundResource(R.drawable.bg_button);
imageButton.setImageResource(mThumbIds[position]);
imageButton.setId(position);

代码示例来源:origin: ybonnel/TransportsRennes

actionButton.setId(id);
actionButton.setLayoutParams(new ViewGroup.LayoutParams((int) mActivity.getResources().getDimension(
    R.dimen.actionbar_compat_height), ViewGroup.LayoutParams.FILL_PARENT));

代码示例来源:origin: stackoverflow.com

else if(v == btnAddChild)
 {
   LinearLayout dynamicLayout = new LinearLayout(this);
   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
   params.weight = 1.0f;
   params.gravity = Gravity.CENTER_VERTICAL;
   dynamicLayout.setLayoutParams(params);
   dynamicLayout.setOrientation(LinearLayout.HORIZONTAL);
   int layoutId = totalDynamicChild + 1000; //dynamicLayout always have dynamic spinnser's id + 1000 to avoid same id
   dynamicLayout.setId(layoutId);
   Spinner spinner = new Spinner(this);
   spinner.setLayoutParams(params);
   spinner.setAdapter(spinChildAdapter);
   spinner.setId(totalDynamicChild);   
   spinnderIdList.add(totalDynamicChild);
   ImageButton btn = new ImageButton(this);
   btn.setImageDrawable(getResources().getDrawable(R.drawable.action_delete));
   btn.setBackgroundResource(0);
   btn.setLayoutParams(params);
   int btnId = totalDynamicChild + 2000; //btn always have dynamic spinnser's id + 2000 to avoid same id
   btn.setId(btnId);
   dynamicLayout.addView(spinner);
   dynamicLayout.addView(btn);
   parentSpinner.addView(dynamicLayout);
   totalDynamicChild++;

代码示例来源:origin: stackoverflow.com

public View getView(int position, View convertView, ViewGroup parent) {
  final ImageButton button;
  if (convertView == null) {
    button = new ImageButton(mContext);
    button.setPadding(2,2,2,2);

  } else {
    button = (ImageButton) convertView;
  }

  button.setId(position);
  button.setImageResource(mThumbIds[position]);
  button.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);

  //Scale button using layout params
  double parentWidth = ((MainActivity) mContext).findViewById(R.id.gridview).getWidth();
  double width = button.getDrawable().getIntrinsicWidth();
  double height = button.getDrawable().getIntrinsicHeight();
  double scalex = parentWidth / parentWidth;
  double scaley = parentWidth / width;
  int newWidth = (int) (width * scalex);
  int newHeight = (int) (height * scaley);

  button.setLayoutParams(new GridView.LayoutParams(newWidth, newHeight));

  return button;
}

相关文章

ImageButton类方法