Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何在布局中添加固定大小的按钮而不变形?_Java_Android_Android Layout - Fatal编程技术网

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

Java Android:如何在布局中添加固定大小的按钮而不变形?,java,android,android-layout,Java,Android,Android Layout,我想创建一个卡片组应用程序,在运行时包含(比如)0到10张卡片 我有一个在父布局中使用XML创建的RelativeLayout,这个RelativeLayout的宽度与父布局匹配,高度与包装内容匹配 现在,我想将固定大小的按钮(卡)添加到该布局中,当没有空间时,它们必须不变形,而是缩放并放置在另一个按钮上 当我添加它们时,会发生以下情况: 但我想实现这样的目标: *对于第二幅图像,我使用了设置边距(xleft,x,x,x);。。。xleft++,但当我有2个或5个按钮时,它们会一直位于另一个按钮

我想创建一个卡片组应用程序,在运行时包含(比如)0到10张卡片

我有一个在父布局中使用XML创建的RelativeLayout,这个RelativeLayout的宽度与父布局匹配,高度与包装内容匹配

现在,我想将固定大小的按钮(卡)添加到该布局中,当没有空间时,它们必须不变形,而是缩放并放置在另一个按钮上

当我添加它们时,会发生以下情况:

但我想实现这样的目标:

*对于第二幅图像,我使用了设置边距(xleft,x,x,x);。。。xleft++,但当我有2个或5个按钮时,它们会一直位于另一个按钮的顶部,而不仅仅是使用空闲空间

所以我在问,是否有一种布局或方法,当没有空间(小屏幕或太多按钮)时,将按钮放在另一个按钮的顶部和旁边,而不是使其变形

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

使用
ConstraintLayout
可以做到这一点,并且与使用嵌套
linearlayout
相比,用户处理所花费的时间更少

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

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


为了阅读更多关于
ConstraintLayout
和更多关于
Chains
好了,伙计们,我尝试了你们的建议,但没能实现我想要的,所以我花了几个小时研究它,最后我做了一些数学和一些函数帮助(最终)找到解决方案。如果有人想尝试,我会发布我的代码:

    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;
    }
}
public void ChangeView(视图){
//在这里,我们放置了要缩放和显示的卡片数量(卡片=按钮)
int CardMax=13;//例如13!!!
//获取手机屏幕dpi的比例(我想是??)
最终浮动比例=getResources().getDisplayMetrics().density;
//得到一些我们需要知道的价值
//**这些值取决于屏幕分辨率或按钮大小**
int layWidth=layout.getWidth();//获取等于屏幕宽度的布局宽度(以像素为单位)
int btnWidth=(int)(50*比例);//设置并缩放任何屏幕的按钮宽度
int maxLayfit=layWidth/btnWidth;//获取可以在不变形的情况下将多少按钮添加到布局中
int layMiddidth=layWidth/2;//获取布局的一半宽度(这有助于从中间开始添加按钮)
int StrMeasur;//此帮助说明如何开始设置按钮
if(CardMax maxLayfit-1){//如果卡的数量大于布局可以容纳的数量
//然后我们计算第一个卡位置(开始/指针)和最后一个卡位置(=布局宽度按钮宽度)
//我们发现,在给定的卡片上,布局必须划分多少相等的部分
//我们得到零件的宽度,将其设置为下一个卡片位置的指针
nextBtn=((铺层宽度开始)-btnWidth-开始)/CardMax;
}否则{
nextBtn=btnWidth;//否则指针只会在另一个按钮旁边设置按钮
}
//这里我们显示所有的按钮
对于(int i=0;i

我相信它可以在任何屏幕上工作

一个选项是为不同的分辨率创建不同的布局。你只能做三个布局,但我不知道这是否是最好的解决方案。mdpi(每英寸中等密度点):依次为160 dpi。hdpi(每英寸高密度点):依次为240 dpi。xhdpi(每英寸超高密度点):依次为320 dpi。当应用程序运行时,用户可以移除或添加卡(按钮),因此我的问题不是命运,而是布局如何包含按钮而不使其变形。使用
ConstraintLayout