0

如何以编程方式对齐按钮左侧的图像视图?在下面的这段代码中,我以编程方式生成按钮和图像视图,但图像视图显示在按钮下方如何设置按钮图像视图的右侧?

public class MainActivity1 extends Activity {
public  static Bitmap yourSelectedImage = null;

ImageView imageView1;
ContentValues newCon = new ContentValues();
public static byte[] blob = null;
public static long reqId;
int i;

  LinearLayout btnLO;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);





     btnLO = new LinearLayout(MainActivity1.this);

     LinearLayout.LayoutParams paramsLO = new LinearLayout.LayoutParams(
                             LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     // button margins
     paramsLO.setMargins(0, 0, 0, 0);
     // button height/width *pixels*
     paramsLO.height = 75;
     paramsLO.width = 75;

     btnLO.setOrientation(LinearLayout.VERTICAL);
     btnLO.setBackgroundColor(5); // not working correctly

     //buttons
     for (i =0;i <j;i++)
     {
     final Button b1 = new Button(MainActivity1.this);
     final ImageView imageView = new ImageView(MainActivity1.this);



     b1.setText(reqdata[i].getSpinnerText());
     b1.setTag(reqdata[i].getValue());
     btnLO.addView(b1, paramsLO);        
     btnLO.addView(imageView, paramsLO);

     b1.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {               
                    reqId =  
   Long.valueOf(reqdata[btnLO.indexOfChild(b1)/2].getValue()).longValue();
                    Toast.makeText(MainActivity1.this, reqId+"", 
                            Toast.LENGTH_SHORT).show();

     }

     final Button b2 = new Button(MainActivity1.this);
     b2.setOnClickListener(new View.OnClickListener() 
     {
         public void onClick(View v) 
         {              
            }
     }); 
     btnLO.addView(b2, paramsLO);
     btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);


     this.addContentView(btnLO, new LayoutParams());







  }
4

1 回答 1

0
LinearLayout li=new LinearLayout(this);
     li.setOrientation(LinearLayout.HORIZONTAL);
     final Button b1 = new Button(MainActivity1.this);
     final ImageView imageView = new ImageView(MainActivity1.this);



     b1.setText(reqdata[i].getSpinnerText());
     b1.setTag(reqdata[i].getValue());
     li.addView(b1, paramsLO);        
     li.addView(imageView, paramsLO);

     btnLO.addView(li);

ScrollView sc=new ScrollView(this); sc.addView(btnLO);

请记住,这必须在 for 循环之外完成。

这就是您需要在 for 循环中向布局中添加按钮和图像的方式

于 2013-02-22T12:02:49.240 回答