完全失去了重量。也许我的代码不对

时间:2014-12-12 05:27:56

标签: java android

所以使用这段代码我得到了理想的结果,因为每次if语句执行时都会创建3个按钮。第一个按钮具有var名称,占据了大部分空间。第二个2按钮,删除和更改,都是相同的大小和名称按钮右侧的小。但是我的体重设定如下:

LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 3.5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 3.5f);

我的印象是,如果数字较小,那么拥有1f的第一个按钮将是最小的。这是我完整的if语句。就像我说的那样,我得到了理想的结果,但我想进一步理解这一点,如果我这样做是对的。感谢

if (count <= 5)
    {
    //CREATE NEW LINEAR LAYOUT
    LinearLayout addTroopLayout = new LinearLayout(this);   

    //CREATE LAYOUT PARAMS FOR LAYOUT
    LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    newLayout.bottomMargin = 10;

    //STYLE NEW LINEAR LAYOUT
    addTroopLayout.setId(count);
    addTroopLayout.setLayoutParams(newLayout);
    addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);     

    //CREATE NEW BUTTONS
    Button newTroop = new Button(this);
    Button remove = new Button(this);
    Button change = new Button(this);

    //CREATE LAYOUT PARAMS FOR BUTTONS
    LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
    LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 3.5f);
    LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 3.5f);


    //STYLE NEW BUTTONS
    newTroop.setId(count);
    newTroop.setText(name);
    newTroop.setGravity(Gravity.LEFT);
    newTroop.setLayoutParams(newTroopParam);
    remove.setId(count);
    remove.setText("-");
    remove.setGravity(Gravity.CENTER_HORIZONTAL);
    remove.setLayoutParams(rmvBtnParam);
    change.setId(count);
    change.setText("...");
    change.setGravity(Gravity.CENTER_HORIZONTAL);
    change.setLayoutParams(chngNameParam);

    //ADD VIEWS TO NEW LAYOUT
    addTroopLayout.addView(newTroop);
    addTroopLayout.addView(remove);
    addTroopLayout.addView(change);

    //ADD NEW LAYOUT TO mainPage LAYOUT
    mainPage.addView(addTroopLayout);

    //Increment Counter
    count++;
    }

1 个答案:

答案 0 :(得分:0)

根据Arnav M的建议,请使用UI进行设计。稍后您可以使用这些属性将代码放在后面。

无论如何,尝试将MATCH_PARENT更改为所有按钮的WRAP_CONTENT。

所以,代码应该是:

LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 3.5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 3.5f);