如何以编程方式对齐按钮左侧的图像视图?

时间:2013-02-22 12:01:11

标签: android

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

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());







  }

1 个答案:

答案 0 :(得分: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);

记住这必须在外面进行循环。

这就是你需要在你的布局中为循环

添加按钮和图像的方法