【问题标题】:How can we add buttons at dynamic positions in layout?我们如何在布局中的动态位置添加按钮?
【发布时间】:2021-01-09 20:16:05
【问题描述】:

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

谁能帮我解决这个问题?

【问题讨论】:

  • 实现 onTouch 事件构造函数。 [1]:stackoverflow.com/questions/3683727/…
  • 嗨 user1285293 你检查我的答案了吗?是你要找的吗?如果不告诉我您需要什么,我会尝试提供更好的答案。如果答案是正确的,您可以将其标记为它。
  • 如果您接受答案或告诉我您需要什么,那就太好了。
  • 嘿 user1285293 你能告诉我我提供的答案是否满足你的要求吗?如果不告诉我你需要什么。如果是这样,您可以将答案标记为正确。
  • 您好 user1285293 请告诉我答案是否适合您。如果是这样,您可以接受答案,否则请告诉我您需要什么。

标签: android


【解决方案1】:

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

你应该得到这样的东西:

【讨论】:

  • 我认为你投了反对票,因为链接在 xml 中做事而不是在活动动态中。所以给出创建和放置动态视图的演示,你可能会被接受+赞成:)
  • 好的。我给了你+1 ..但你仍然需要做一件事..显示一些OP可以使用的方法将其放置在布局中的任何位置,如top..bottom..center..padding ..margin...无论何时你有时间可以编辑答案..我相信这可能是一个很好的答案..!
  • 感谢您推动我给出更好的答案
猜你喜欢
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
相关资源
最近更新 更多