添加按钮动态地根据屏幕宽度 [英] Add Buttons dynamically depending on screen width

查看:109
本文介绍了添加按钮动态地根据屏幕宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态添加按钮,根据屏幕的宽度。

I'm trying to add buttons dynamically depending on screen width.

即。如果我得到6个按钮,然后我需要给他们相应的位置,这样的按钮出现在与父母左,右家长等间隔的中心。

i.e. if I get 6 buttons then I need to position them accordingly, so that the buttons appear at the center with equal spacings on left parent and right parent.

下面是一块code的,我正在努力,但没有任何结果:

Here is the piece of code which I'm trying but no result:

private void btmBarBtns(int position) {

    RelativeLayout rlLayout;
    RelativeLayout.LayoutParams layoutParams;

    int leftMargin = scrWidth/pageCount;

    CommonMethods.getSystemOutput("Left Margin::::"+leftMargin);

    for (int i = 0; i < pageCount; i ++ ) {

        rlLayout = (RelativeLayout) findViewById(R.id.ivBottomBar);

        layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        layoutParams.leftMargin = leftMargin;

        ib = new ImageButton(this);
        ib.setId(i);
        ib.setLayoutParams(layoutParams);
        ib.setBackgroundResource(R.drawable.white_circle_32x32);

        rlLayout.addView(ib);
        leftMargin = leftMargin + 70;

        if (ib.getId() == position) {
            ib.setBackgroundResource(R.drawable.black_circle_32x32);
        }

    }
}

在上面的code我有高度25dp和宽度FILL_PARENT相对布局。我能够添加的按钮,但它们不位于中央

In the above code I have a Relative layout with height 25dp and width fill_parent. I am able to add the buttons but they are not positioned at the center.

推荐答案

如果你想为中心的 ImageButtons 具有同等空间的左右,那么你可以简单的总结他们在一个的LinearLayout ,然后中心认为的LinearLayout RelativeLayout的

If all you want to is center those ImageButtons with equal space left and right then you could simple wrap them in a LinearLayout and then center that LinearLayout in the parent RelativeLayout:

    RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent);
    LinearLayout container = new LinearLayout(this);
    for (int i = 0; i < 5; i++) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ImageButton ib = new ImageButton(this);
        ib.setId(i);
        ib.setLayoutParams(layoutParams);
        ib.setBackgroundResource(R.drawable.ic_launcher);
        container.addView(ib);

        if (ib.getId() == position) {
            ib.setBackgroundResource(R.drawable.black_circle_32x32);
        }
    }
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,
            RelativeLayout.TRUE);
    rlLayout.addView(container, layoutParams);

如果你想多写code只是上面做,那么你可以修改当前的布局,并添加这个元素作为主播:

If you want to write more code just to do the above then you could modify your current layout and add this element as an anchor:

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_centerHorizontal="true"
    android:id="@+id/anchor" />

,然后在code位置 ImageButtons 这个锚查看的左侧和右侧:

int anchorId = R.id.anchor;     
        int btnsNr = 6; // this is the number of Buttons
        RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent);
        if (btnsNr % 2 != 0) {
            anchorId = 1000;
            btnsNr--;
            ImageButton imgb = new ImageButton(this);
            imgb.setImageResource(R.drawable.shop_open);
            imgb.setId(anchorId);
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            rlLayout.addView(imgb, rlp);
        }
        int whichPart = 1;
        while (whichPart >= 0) {
            int previousId = anchorId;
            for (int i = 0; i < (btnsNr / 2); i++) {
                RelativeLayout.LayoutParams tmp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                if (whichPart == 1) {
                    tmp.addRule(RelativeLayout.LEFT_OF, previousId);
                } else {
                    tmp.addRule(RelativeLayout.RIGHT_OF, previousId);
                }
                ImageButton imgb = new ImageButton(this);
                previousId += whichPart == 1 ? -1 : 1;
                imgb.setId(previousId);
                imgb.setImageResource(R.drawable.shop_open);
                rlLayout.addView(imgb, tmp);
            }
            whichPart--;
        }

如果你要计算的数量 ImageButtons 适合屏幕(和水平居中他们),你应该提到。

If you want to calculate the number of ImageButtons that fit the screen(and center them horizontally) you should have mentioned.

这篇关于添加按钮动态地根据屏幕宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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