【问题标题】:TableLayout / Rows / TextView marginTableLayout / Rows / TextView 边距
【发布时间】:2013-11-10 20:11:53
【问题描述】:

我有带有行的表格布局,并且在每一行中都有一堆文本视图(全部以编程方式添加,没有 xml),我想要网格线,所以我试图通过 laoyoutparams 添加边距,但它看起来像 TableLayout.LayoutParams 不适用到 textviews 只到行,所以我在行上得到一个边距,但在单元格上没有。我也尝试过在单元格上使用 RelativeLaout.LayoutParams,但 thaqt 也不起作用。任何人都可以为我的问题提出任何解决方案?

【问题讨论】:

    标签: android tablelayout gridlines


    【解决方案1】:

    您应该使用正确的LayoutParams 进行查看。 在添加视图时:

    1. TableLayout;使用TableLayout.LayoutParams
    2. TableRow;使用TableRow.LayoutParams

    编辑:我已经编辑了代码。您希望某些单元格有一些黑色空间。您可以通过为该视图设置相同的背景颜色(或为行和单元格上的视图设置透明背景)来实现这一点。我认为使用 tableLayout 为 textViews 设置相同的 bg 颜色更容易。

    这是一个完整的代码示例,您可以尝试(添加颜色以便轻松查看输出):

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TableLayout tableLayout = createTableLayout(16, 14);
        setContentView(tableLayout);
    
        makeCellEmpty(tableLayout, 1, 6);
        makeCellEmpty(tableLayout, 2, 3);
        makeCellEmpty(tableLayout, 3, 8);
        makeCellEmpty(tableLayout, 3, 2);
        makeCellEmpty(tableLayout, 4, 8);
        makeCellEmpty(tableLayout, 6, 9);
    
    }
    
    public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
        // get row from table with rowIndex
        TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
    
        // get cell from row with columnIndex
        TextView textView = (TextView)tableRow.getChildAt(columnIndex);
    
        // make it black
        textView.setBackgroundColor(Color.BLACK);
    }
    
    private TableLayout createTableLayout(int rowCount, int columnCount) {
        // 1) Create a tableLayout and its params
        TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setBackgroundColor(Color.BLACK);
    
        // 2) create tableRow params
        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
        tableRowParams.setMargins(1, 1, 1, 1);
        tableRowParams.weight = 1;
    
        for (int i = 0; i < rowCount; i++) {
            // 3) create tableRow
            TableRow tableRow = new TableRow(this);
            tableRow.setBackgroundColor(Color.BLACK);
    
            for (int j= 0; j < columnCount; j++) {
                // 4) create textView
                TextView textView = new TextView(this);
                textView.setText(String.valueOf(j));
                textView.setBackgroundColor(Color.WHITE);
                textView.setGravity(Gravity.CENTER);
    
                // 5) add textView to tableRow
                tableRow.addView(textView, tableRowParams);
            }
    
            // 6) add tableRow to tableLayout
            tableLayout.addView(tableRow, tableLayoutParams);
        }
    
        return tableLayout;
    }
    

    下面是这段代码的输出,我们可以看到边距已正确应用:

    【讨论】:

    • 谢谢 aegean 如果我需要 textView1 和 textView 2 之间的边距(有一些蓝色区域)怎么办?
    • 可以给tableRowparams添加边距:tableRowParams.setMargins(10, 20, 10, 20);
    • 这是合乎逻辑的,但它不适用于我的设置,我的行有 13 个文本视图和一些按钮,所以我实际上想要正常的网格但没有任何效果。我在某处读到您需要为按钮设置它,因为它们代表列,但也不知道如何设置它
    • 对不起,我听不懂。在 tableLayout 你有 tableRows。如果你直接把一个视图放到tableRow中,它会横向添加到tableRow中。如果你想要不同的东西,请使用 gridLayout。分享你所做的,你的代码。或分享您想要的图像,可能会更清晰。
    • 查看我编辑的答案。我想您会明白这一点并根据您的需要管理代码。
    猜你喜欢
    • 2019-12-10
    • 1970-01-01
    • 2020-09-22
    • 2013-07-19
    • 2016-11-09
    • 2012-01-17
    • 2011-06-16
    • 2012-10-27
    • 2014-06-07
    相关资源
    最近更新 更多