Android:如何在布局中添加programmaticaly固定大小按钮而不会变形?

时间:2017-04-27 16:28:15

标签: java android android-layout

我想创建一个Card Deck应用程序,其中包含(假设)运行时总共0到10张卡片。

我在父布局中使用XML创建了RelativeLayout,此RelativeLayout的宽度与父级和高度相匹配以包装内容。

现在我想在该布局中添加固定大小的按钮(卡片),当没有空间时,他们必须不变形,但需要缩放和放置另一方面。

当我添加它们时,它会发生:https://postimg.org/image/ic8pmaju7/

但我希望实现这样的目标:https://postimg.org/image/567dj49j9/

*对于第二张图片,我使用 setmargins(xleft,x,x,x); ... xleft ++; ,但当我有2或5个按钮时,他们会保留在另一个按钮的顶部,而不是仅使用空闲空间。

所以即时通讯询问是否有布局或方法,当没有空间(小屏幕或太多按钮)时,将按钮放在另一个按钮的顶部和旁边,而不是将它们变形。

2 个答案:

答案 0 :(得分:0)

  

ConstraintLayout允许您使用平面视图层次结构创建大型复杂布局(无嵌套视图组)。它与RelativeLayout类似,因为所有视图都根据兄弟视图和父布局之间的关系进行布局,但它比RelativeLayout更灵活,更易于与Android Studio的布局编辑器一起使用。

使用ConstraintLayout,您可以执行此操作,并且比使用嵌套LinearLayouts的用户处理更少。

要将按钮放在按钮旁边并根据屏幕大小调整它们,您可以使用Chains,这是ConstraintLayout的资源。

  

链在单个轴(水平或垂直)中提供类似行的行为。另一个轴可以独立约束。

Click here详细了解ConstraintLayoutClick here,详细了解Chains

答案 1 :(得分:0)

好吧,伙计们我尝试了你的建议,但我无法实现我想要的东西,所以我花了HOURS来研究它,我最后得到了一些数学和一些功能的帮助(最后)找到解决方案。如果有人想尝试,我会发布我的代码:

    public void ChangeView(View view) {
    //here we put number of cards that we want to be scaled and displayed (cards=buttons)
    int CardMax=13;//13 for example!!!

    //getting scale for dpi of phone screen(i think??)
    final float scale = getResources().getDisplayMetrics().density;
    // getting some values we need to know
    // **THESE VALUES DEPEND FROM SCREEN RESOLUTION OR THE SIZE OF BUTTON**
    int layWidth = layout.getWidth();  //getting layout width that equals screen width(in pixels)
    int btnWidth = (int) (50 * scale); //setting and scaling the width of the button for any screen
    int maxLayfit=layWidth/btnWidth;   //getting how many buttons can be added to layout without deformation
    int layMidWidth = layWidth / 2;    //getting layouts half width (that helps to start adding buttons from middle)
    int StrMeasur;                     // this help how to start setting the buttons
    if (CardMax <= maxLayfit) {        // if cards are less than Layout can hold without deformation
        StrMeasur = CardMax;           // StrMeasur equals number of cards
    } else {
        StrMeasur = maxLayfit;         // else equals max number of cards without deformation
    }
    int pointer=0;                     //pointer that will say where to put the first button
    pointer = layMidWidth - ((btnWidth / 2) * StrMeasur);//depends of how many cards we have and button width
    int start =layMidWidth-((btnWidth / 2) * StrMeasur);
    int nextBtn = 0;                   //nextBtn says where to put the next button
    //here we set the buttons on top and next to the previous button as we need **TRICKY PART**
    if (CardMax > maxLayfit-1) {       //if number of cards is bigger than the number the layout can hold
                                       //then we calculate first card position (start/pointer) and the last card position(=layout width-button width)
                                       //we find how many equal parts the layout has to be divided with the given cards
                                       //and we get the width of the part to set it as pointer of the next card place
        nextBtn =(((layWidth-start)-btnWidth)-start)/CardMax;
    }else{
        nextBtn=btnWidth;              //else the pointer just set buttons next to the other
    }
    //Here we display all the buttons
    for (int i = 0; i < CardMax; i++) {
        Button cards = new Button(this);
        cards.setWidth(btnWidth);
        cards.setHeight((int) (90 * scale));
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params2.setMargins(pointer, 0, 0, 0);
        cards.setLayoutParams(params2);
        cards.setText("" + (i + 1));
        cards.setId(i);
        layout.addView(cards);
        pointer = pointer + nextBtn;
    }
}

我相信它可以在任何屏幕上使用。