android - 我们如何在布局中的动态位置添加按钮?

标签 android

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

谁能帮我解决这个问题?

最佳答案

使用 RelativeLayout 将控件放置在您喜欢的位置。看看这个链接: Dynamic TextView in Relative layout

还有这里 How to create a RelativeLayout programmatically with two buttons one on top of the other?

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

这是一个如何使用 RelativeLayout 动态定位控件的示例:

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

你应该得到这样的东西:

enter image description here

关于android - 我们如何在布局中的动态位置添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12524589/

相关文章:

android - Opencv 的 javacameraview 无法在 Android Marshmallow 设备上打开相机

Android浏览器点击绿色边框

java - 3d 瓦片(和 2d Sprite )的等距渲染

android - 如何以编程方式为 TextView 提供 5dp 行距

android - 内存不足异常 : Load Bunch Of Images From Server

Android 支持库 SearchView xml 属性 queryHint 和 iconifiedByDefault 不起作用

android - Galaxy s3 有温度传感器吗?

android - 为什么 BroadcastReceiver 被不必要地调用?

android - setVisibility(View.INVISIBLE) 有什么区别;设置可见性(0);

android - 添加非 Jar 格式的库