[英]How to set button position programmatically?
In my code I create button two programmatically how do I set is position to center of screen programmatically?在我的代码中,我以编程方式创建了两个按钮,如何以编程方式将位置设置为屏幕中心? This is my image of screen https://imgur.com/7wdyVc6 submit button(button2) is create just below forloop button how do I set is position to center of screen?这是我的屏幕图像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());
}
One possible way is to use the RelativeLayout
as your base layout and add all other children views to this layout using the RelativeLayout.LayoutParams
class for setting the layout parameters.一种可能的方法是使用RelativeLayout
作为基本布局,并使用RelativeLayout.LayoutParams
类将所有其他子视图添加到此布局以设置布局参数。
The RelativeLayout.Layoutparams.left
and RelativeLayout.Layoutparams.top
attributes can be your (x,y) co- ordinates on the base view. RelativeLayout.Layoutparams.left
和RelativeLayout.Layoutparams.top
属性可以是您在基础视图上的 (x,y) 坐标。
in your activity xml, copy paste the following in the desired button component:在您的活动 xml 中,将以下内容复制粘贴到所需的按钮组件中:
android:layout_marginBottom="147dp"
android:layout_marginTop="50dp"
android:layout_marginleft="50dp"
android:layout_marginRight="47dp"
play only with the numbers and I'm sure u'll get the hang of it :)只玩数字,我相信你会掌握它的窍门:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.