Android 将按钮动态放置在RelativeLayout中的特定位置
我正在尝试将几个按钮动态放置在Android 将按钮动态放置在RelativeLayout中的特定位置,android,android-relativelayout,Android,Android Relativelayout,我正在尝试将几个按钮动态放置在RelativeLayout中。问题是,即使x和y坐标计算正确,所有按钮都放在同一个位置。使用LayoutParams和设置marginRight,marginBottom指定坐标是否正确 代码: layout.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener()) { @凌驾 公共图书馆 { layout.getViewTreeObserver().RemoveGloba
RelativeLayout
中。问题是,即使x和y坐标计算正确,所有按钮都放在同一个位置。使用LayoutParams
和设置marginRight
,marginBottom
指定坐标是否正确
代码:
layout.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener())
{
@凌驾
公共图书馆
{
layout.getViewTreeObserver().RemoveGlobalOnlyOutliner(此文件);
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(按钮宽度、按钮高度);
int currentX=20;
电流y=20;
对于(产品:controller.getProducts(“pizza”)){
Log.d(标签“CurrentY:+CurrentY”);
Log.d(标签“CurrentX:+CurrentX”);
Button tempButton=新建按钮(getActivity());
setId(product.getId());
tempButton.setText(product.getName());
layoutParams.rightMargin=currentX;
layoutParams.bottomMargin=currentY;
tempButton.setLayoutParams(layoutParams);
layout.addView(临时按钮);
if(layout.getWidth()
我发现了错误。似乎每次循环时我都必须重新实例化LayoutParams
,在使用相同的LayoutParams
实例时仅设置边距属性是不够的。我认为它会在调用addView()
方法后立即将按钮添加到指定的布局中,但实际上它会在最后(当方法完成时)添加按钮,因为它将所有按钮放置到相同(最后)的坐标
代码:
layout.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener())
{
@凌驾
公共图书馆
{
layout.getViewTreeObserver().RemoveGlobalOnlyOutliner(此文件);
RelativeLayout.LayoutParams LayoutParams;
int currentX=20;
电流y=20;
对于(产品:controller.getProducts(“pizza”)){
layoutParams=新的RelativeLayout.layoutParams(按钮宽度、按钮高度);
Button tempButton=新建按钮(getActivity().getApplicationContext());
setId(product.getId());
tempButton.setText(product.getName());
layoutParams.setMargins(currentX,currentY,0,0);
tempButton.setLayoutParams(layoutParams);
layout.addView(临时按钮);
if(layout.getWidth()
我发现了错误。似乎每次循环时我都必须重新实例化LayoutParams
,在使用相同的LayoutParams
实例时仅设置边距属性是不够的。我认为它会在调用addView()
方法后立即将按钮添加到指定的布局中,但实际上它会在最后(当方法完成时)添加按钮,因为它将所有按钮放置到相同(最后)的坐标
代码:
layout.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener())
{
@凌驾
公共图书馆
{
layout.getViewTreeObserver().RemoveGlobalOnlyOutliner(此文件);
RelativeLayout.LayoutParams LayoutParams;
int currentX=20;
电流y=20;
对于(产品:controller.getProducts(“pizza”)){
layoutParams=新的RelativeLayout.layoutParams(按钮宽度、按钮高度);
Button tempButton=新建按钮(getActivity().getApplicationContext());
setId(product.getId());
tempButton.setText(product.getName());
layoutParams.setMargins(currentX,currentY,0,0);
tempButton.setLayoutParams(layoutParams);
layout.addView(临时按钮);
if(layout.getWidth()
如何获得按钮\u宽度、按钮\u高度、边距\u底部边距\u左侧
我还可以动态放置多个按钮。你能帮帮我吗。你的代码真的对我很有帮助。请进一步指导我。我是android新手。如何获得按钮\u宽度、按钮\u高度、边距\u底部边距\u左侧
我还需要动态放置多个按钮。你能帮帮我吗。你的代码真的对我很有帮助。请进一步指导我。我是android新手。
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(BUTTON_WIDTH, BUTTON_HEIGHT);
int currentX = 20;
int currentY = 20;
for (Product product: controller.getProducts("pizza")){
Log.d(TAG, "CurrentY: " + currentY);
Log.d(TAG, "CurrentX: " + currentX);
Button tempButton = new Button(getActivity());
tempButton.setId(product.getId());
tempButton.setText(product.getName());
layoutParams.rightMargin = currentX;
layoutParams.bottomMargin = currentY;
tempButton.setLayoutParams(layoutParams);
layout.addView(tempButton);
if (layout.getWidth() < currentX + MARGIN_LEFT + BUTTON_WIDTH){
currentX = 20;
currentY += BUTTON_HEIGHT + MARGIN_BOTTOM;
}
else{
currentX += MARGIN_LEFT + BUTTON_WIDTH;
}
}
}
});
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
RelativeLayout.LayoutParams layoutParams;
int currentX = 20;
int currentY = 20;
for (Product product: controller.getProducts("pizza")){
layoutParams = new RelativeLayout.LayoutParams(BUTTON_WIDTH, BUTTON_HEIGHT);
Button tempButton = new Button(getActivity().getApplicationContext());
tempButton.setId(product.getId());
tempButton.setText(product.getName());
layoutParams.setMargins(currentX, currentY, 0, 0);
tempButton.setLayoutParams(layoutParams);
layout.addView(tempButton);
if (layout.getWidth() < currentX + MARGIN_LEFT + (2 * BUTTON_WIDTH)){
currentX = 20;
currentY += BUTTON_HEIGHT + MARGIN_BOTTOM;
}
else{
currentX += MARGIN_LEFT + BUTTON_WIDTH;
}
}
//layout.requestLayout();
}
});