在我的代码中,我以编程方式创建按钮2如何以编程方式设置位置到屏幕中心?这是我的屏幕https://imgur.com/7wdyVc6提交按钮(button2)的图像是在forloop按钮下方创建的,我如何设置屏幕中心的位置?
ScrollView scroll = new ScrollView(MainActivity1.this);
scroll.setLayoutParams(new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
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 < reqdata.length;i++)
{
LinearLayout li=new LinearLayout(this);
li.setOrientation(LinearLayout.HORIZONTAL);
final Button b1 = new Button(MainActivity1.this);
final ImageView imageView = new ImageView(MainActivity1.this);
li.addView(b1, paramsLO);
li.addView(imageView, paramsLO);
btnLO.addView(li);
b1.setText(reqdata[i].getSpinnerText());
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
imageView1 = imageView;
Intent pictureActionIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD,IMAGE_CAPTURE_NAME)));
startActivityForResult(pictureActionIntent,CAMERA_PICTURE);
b1.setClickable(false);
}
});
}
final Button b2 = new Button(MainActivity1.this);
b2.setText("Submit");
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
long visitID = dbConnector.saveVisit();
for (i =0;i <reqdata.length;i++)
{
dbConnector.saveVisitDetail(listByte.get(i),visitID,Long.valueOf(reqdata[i]
.getValue()).longValue());
}
}
});
btnLO.addView(b2, paramsLO);
btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
scroll.addView(btnLO);
this.addContentView(scroll, new LayoutParams());
}
答案 0 :(得分:2)
一种可能的方法是使用RelativeLayout
作为基本布局,并使用RelativeLayout.LayoutParams
类将所有其他子视图添加到此布局以设置布局参数。
RelativeLayout.Layoutparams.left
和RelativeLayout.Layoutparams.top
属性可以是您的(x,y)合作
纵坐标。
答案 1 :(得分:0)
在您的活动xml中,将以下内容复制粘贴到所需的按钮组件中:
android:layout_marginBottom="147dp"
android:layout_marginTop="50dp"
android:layout_marginleft="50dp"
android:layout_marginRight="47dp"
只播放数字,我相信你会得到它的信息:)