【问题标题】:Add Buttons dynamically depending on screen width根据屏幕宽度动态添加按钮
【发布时间】:2012-06-17 07:31:25
【问题描述】:

我正在尝试根据屏幕宽度动态添加按钮。

即如果我有 6 个按钮,那么我需要相应地定位它们,以便按钮出现在中心,左父和右父的间距相等。

这是我正在尝试但没有结果的一段代码:

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

    }
}

在上面的代码中,我有一个相对布局,高度为 25dp,宽度为 fill_parent。我可以添加按钮,但它们不在中心位置。

【问题讨论】:

    标签: android button dynamic


    【解决方案1】:

    如果您只想将那些ImageButtons 以左右相等的空间居中,那么您可以简单地将它们包装在LinearLayout 中,然后将LinearLayout 居中在父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);
    

    如果您想编写更多代码来完成上述操作,那么您可以修改当前布局并将此元素添加为锚点:

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

    然后在代码中将ImageButtons 定位到此锚点View 的左右两侧:

    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 的数量(并将它们水平居中),您应该提到。

    【讨论】:

    • 我使用了一个相对布局(子到父相对布局),其高度和宽度的内容都被包裹起来,将其对齐到中心并添加了按钮。我认为没有必要添加视图容器。我通过指定边距动态添加了按钮。它对我有用。谢谢你的帮助!!!
    • @krisDrOid 添加View 是另一种方法(使用更多代码)。我使用了LinearLayout,当然你也可以像你一样使用RelativeLayout
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2016-08-28
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多