如何在布局中的动态位置添加按钮? [英] How can we add buttons at dynamic positions in layout?

查看:221
本文介绍了如何在布局中的动态位置添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在布局或使用画布而不是表格布局的动态位置添加按钮?

How can we add buttons at dynamic positions in layout or using canvas, not in table layout?

有人可以帮我吗?

推荐答案

使用RelativeLayout将控件放置在您喜欢的位置.看一下这个链接: 相对布局中的动态TextView

Use RelativeLayout to position your controls where you like them. Have a look at this link: Dynamic TextView in Relative layout

在这里 如何以编程方式创建一个带有两个按钮的RelativeLayout,一个位于另一个按钮之上?

如果您只想在XML中实现它.看这里: http://www.mkyong.com/android/android-relativelayout-example/

If you like to achieve it within XML only. Look here: http://www.mkyong.com/android/android-relativelayout-example/

下面是一个示例,说明如何使用RelativeLayout动态放置控件:

Here an example how you could use the RelativeLayout to position your controls dynamically:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Creating a new RelativeLayout
        RelativeLayout mainRelativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters with Fill_Parent
        RelativeLayout.LayoutParams relativeLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

        // Creating a new Left Button
        Button buttonLeft = new Button(this);
        buttonLeft.setText("Left");

        // Creating a new Left Button with Margin
        Button buttonLeftWithMargin = new Button(this);
        buttonLeftWithMargin.setText("Left with Margin");

        // Creating a new Center Button
        Button buttonCenterParent = new Button(this);
        buttonCenterParent.setText("Center");

        // Creating a new Bottom Button
        Button buttonBottom = new Button(this);
        buttonBottom.setText("Bottom");

        // Add a Layout to the Buttons
        AddButtonLayout(buttonLeft, RelativeLayout.ALIGN_PARENT_LEFT);
        AddButtonLayout(buttonCenterParent, RelativeLayout.CENTER_IN_PARENT);
        AddButtonLayout(buttonBottom, RelativeLayout.ALIGN_PARENT_BOTTOM);

        // Add a Layout to the Button with Margin
        AddButtonLayout(buttonLeftWithMargin, RelativeLayout.ALIGN_PARENT_LEFT, 30, 80, 0, 0);

        // Add the Buttons to the View
        mainRelativeLayout.addView(buttonLeft);
        mainRelativeLayout.addView(buttonCenterParent);
        mainRelativeLayout.addView(buttonBottom);
        mainRelativeLayout.addView(buttonLeftWithMargin);

        // Setting the RelativeLayout as our content view
        setContentView(mainRelativeLayout, relativeLayoutParameters);
    }

    private void AddButtonLayout(Button button, int centerInParent, int marginLeft, int marginTop, int marginRight, int marginBottom) {
        // Defining the layout parameters of the Button
        RelativeLayout.LayoutParams buttonLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        // Add Margin to the LayoutParameters
        buttonLayoutParameters.setMargins(marginLeft, marginTop, marginRight, marginBottom);

        // Add Rule to Layout
        buttonLayoutParameters.addRule(centerInParent);

        // Setting the parameters on the Button
        button.setLayoutParams(buttonLayoutParameters);     
    }

    private void AddButtonLayout(Button button, int centerInParent) {
        // Just call the other AddButtonLayout Method with Margin 0
        AddButtonLayout(button, centerInParent, 0 ,0 ,0 ,0);
    }
}

您应该得到这样的东西:

And you should get something like this:

这篇关于如何在布局中的动态位置添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆