以编程方式缩小按钮

时间:2016-03-04 14:13:37

标签: android android-layout android-button android-tablelayout

我需要在我的应用程序中动态填充表格,当我用XML创建布局时,我通过乱搞并使字段缩放得到了正确的结果,但是当我尝试以编程方式执行此操作时,我得到了这个。

The problem

正如您所看到的那样,在底部被切断的那个仍然是用XML生成的,它与标题栏完全匹配。
前三个是以编程方式添加的,并没有做他们应该做的事情。

如何让按钮在代码中没有最小宽度?

这是我的行生成功能:

private void generateLocationRow(){
    TableLayout tableLocation = (TableLayout) findViewById(R.id.info_table_locations);
    TableRow tableLocationTitleRow = (TableRow) findViewById(R.id.info_table_locations_titles);
    LayoutParams params;
    //create counter for index in table
    int c = 3;
    for(Location l: locations){
        //makes Rows
        TableRow tRow = new TableRow(this);
        tRow.setBackgroundResource(R.color.colorBlueLight);
        tRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        TableRow tDivider = new TableRow(this);

        //Makes 5 bottomBorders and puts them in an Array
        int bottomBorderCount = tableLocationTitleRow.getChildCount();
        View[] bottomBorders = new View[bottomBorderCount];

        for (int i = 0; i < bottomBorderCount; i++) {
            View bottomBorder = new View(this);
            bottomBorder.setBackgroundResource(R.color.colorDarkGray);
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()); //converts dp to px
            bottomBorder.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));
            bottomBorder.setLayoutParams(bottomBorder.getLayoutParams());

            bottomBorders[i] = bottomBorder;
        }

        //makes 3 sideBorders and puts them in an Array
        int sideBorderCount = (bottomBorderCount / 2) + (bottomBorderCount % 2);
        View[] sideBorders = new View[sideBorderCount];

        for (int i = 0; i < sideBorderCount; i++){
            View sideBorder = new View(this);
            sideBorder.setBackgroundResource(R.color.colorDarkGray);
            int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()); //converts dp to px
            sideBorder.setLayoutParams(new LayoutParams(width, LayoutParams.FILL_PARENT));
            sideBorder.setLayoutParams(sideBorder.getLayoutParams());

            sideBorders[i] = sideBorder;
        }

        //makes columnInfoButton
        Button btnInfo = new Button(this);
        LayoutParams paramsButton = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsButton.gravity = Gravity.CENTER;
        btnInfo.setPadding(2,2,2,2);
        btnInfo.setMinHeight(0);
        btnInfo.setMinWidth(0);
        btnInfo.setText(R.string.info_button_info);
        btnInfo.setLayoutParams(paramsButton);

        //makes columnType
        TextView txtType = new TextView(this);
        params = new LayoutParams();
        params.setMargins(10, 0, 10, 0);
        params.gravity = Gravity.CENTER;
        txtType.setLayoutParams(params);
        txtType.setPadding(5,5,5,5);
        txtType.setText(String.valueOf(l.getType()));

        //makes columnName
        TextView txtName = new TextView(this);
        txtName.setPadding(5, 5, 5, 5);
        txtName.setLayoutParams(params);
        txtName.setText(String.valueOf(l.getName()));

        //makes columnAddress
        TextView txtAddress = new TextView(this);
        txtAddress.setPadding(5, 5, 5, 5);
        txtAddress.setLayoutParams(params);
        txtAddress.setText(String.valueOf(l.getAddress() + " " + l.getTown()));

        //puts content in TableRow
        tRow.addView(sideBorders[0]);
        tRow.addView(btnInfo);
        tRow.addView(sideBorders[1]);
        tRow.addView(txtType);
        tRow.addView(sideBorders[2]);
        tRow.addView(txtName);
        tRow.addView(sideBorders[3]);
        tRow.addView(txtAddress);
        tRow.addView(sideBorders[4]);

        //makes divider
        for (int i = 0; i < bottomBorderCount; i++){
            tDivider.addView(bottomBorders[i]);
        }

        tableLocation.addView(tRow, c);
        tableLocation.addView(tDivider, c+1);

        c = c+2;
    }
}

任何帮助都将不胜感激。

0 个答案:

没有答案