Android在页面上传播TableLayout

时间:2017-03-10 11:01:44

标签: android android-tablelayout

我正在尝试让我的TableLayout遍布整个页面。现在所有的TableRows都相互“推”。但是我希望他们能够进一步分开。

现在的情况:

enter image description here

我想要实现的目标:

enter image description here

Activity.java(因为我正在动态添加TableRows而添加了这个)

    #include<iostream>
using std::cout;
int main(int a)
{
int* test;
test = &a;
cout<<"Value of a is "<<*test<<"\n";
cout <<test<<" is the memory address of a\n";
}

layout.xml

public class FoundLocationsActivity extends AppCompatActivity{

    int tableRows = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foundlocations);
        this.fillTable();
    }

    private void fillTable() {
        //Get  the tablelayout
        TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);

        //Make the columns
        String[] columns = new String[3];
        columns[0] = "Sportschool";
        columns[1] = "Afstand";
        columns[2] = "Score";

        this.addTableRow(tableLayout, columns);

        //Insert the locations hardcoded (for now).
        String[] fitar = new String[3];
        fitar[0] = "Fitar";
        fitar[1] = "12km";
        fitar[2] = "5.0";

        String[] rodea = new String[3];
        rodea[0] = "Rodea";
        rodea[1] = "6km";
        rodea[2] = "4.0";

        String[] mostreu = new String[3];
        mostreu[0] = "Mostreu";
        mostreu[1] = "30km";
        mostreu[2] = "3.0";

        addTableRow(tableLayout, fitar);
        addTableRow(tableLayout, rodea);
        addTableRow(tableLayout, mostreu);
    }

    private void addTableRow(TableLayout tableLayout, String[] text) {
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));

        for (int i = 0; i < text.length; i++) {
            TextView column = new TextView(this);
            column.setId(tableRows);
            column.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
            column.setTextColor(Color.BLACK);
            column.setText(text[i]);
            tableRow.addView(column);
        }

        //tableRow.setLayoutParams();
        tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        tableRows++;
    }
}

提前致谢!

2 个答案:

答案 0 :(得分:1)

我设法使用这个答案修复它:

https://stackoverflow.com/a/14941587/4653908

在layout.xml中,将以下内容添加到TableLayout:

var value = {2:1,53:2,56:4,57:9,61:2,62:16,63:2,398:24};

function getKeysWithHighestValue(o, n){
  var keys = Object.keys(o);
  keys.sort(function(a,b){
    return o[b] - o[a];
  })
  console.log(keys);
  return keys.slice(0,n);
}

console.log(getKeysWithHighestValue(value, 4))

答案 1 :(得分:-1)

如果您确认仅使用3列,那么为什么不在.xml文件中设置大小?使用类似的东西:

<TableRow>
<TextView>
set your size
</TextView>
</TableRow>