如何动态添加不同的按钮

发布于 2024-11-30 21:17:50 字数 275 浏览 0 评论 0原文

我有一个按钮数组(不同大小等),它们是从 xml 文件(由我编写)配置的。我想在屏幕底部添加这些按钮,当按钮行结束时,只需开始一个新行并添加按钮,直到数组结束。我想提一下,我没有在 xml 文件中设置按钮的大小,所以我从一开始就不知道按钮的大小。另一个问题是,在我使用layout.addView(button)以编程方式将按钮添加到布局之后或之前,方法button.getWidth()返回0,因为UI元素尚未在UI中绘制。我还重写了 onLayout() 方法,但仍然无法重绘按钮。

如果您有任何想法,请帮忙。 谢谢

I have an array of Buttons (different sizes etc) which are configured from and xml file (written by me). I want to add those buttons on the bottom of the screen and when the row of buttons ends, just start a new row and add buttons until the array ends. I want to mention that I do not set the size of the buttons in the xml file so I don't know the size from the beginning. Another problem is that after or before I add the button to the layout programatically with layout.addView(button) the method button.getWidth() returns 0 because the UI elements are not drawn in the UI yet. I also overrided the onLayout() method but still wasn't able to redraw the buttons.

If you have any ideas, please help.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云胡 2024-12-07 21:17:50

那么您可以使用权重属性使按钮具有相同的大小。在这种情况下,请确定一行中需要多少个按钮,并使用 for 循环来相应地执行操作。如果您需要宽度,您可以使用类似的方法

将其添加到您的 onCreate 中

ViewTreeObserver vto        =   layout.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    You should be able to get the width and height
                    over here.

                    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });

Well you could make the button take the same size using weight property. In that case, decide how many buttons you need in a row and use a for loop to do it accordingly. If you need the width you could use something like

Add this to your onCreate

ViewTreeObserver vto        =   layout.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    You should be able to get the width and height
                    over here.

                    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
巨坚强 2024-12-07 21:17:50

我就是这样做的。我重写了包含按钮的布局的 onMeasure() 方法。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specSize =  MeasureSpec.getSize(widthMeasureSpec);
        this.width = specSize;

        specSize =  MeasureSpec.getSize(heightMeasureSpec);
        this.height = specSize;

        //now you can use the sizes before the layout is drawn
        this.webview.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,this.height - this.menuBarHeight) );

        //don't forget for the parent method! 
        //but at the end, after the measures are done
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);   
    }

This is how I did it. I overrided the onMeasure() method of the layout containing the buttons.

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specSize =  MeasureSpec.getSize(widthMeasureSpec);
        this.width = specSize;

        specSize =  MeasureSpec.getSize(heightMeasureSpec);
        this.height = specSize;

        //now you can use the sizes before the layout is drawn
        this.webview.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,this.height - this.menuBarHeight) );

        //don't forget for the parent method! 
        //but at the end, after the measures are done
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);   
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文