How can I get an Android TableLayout to fill the screen?

后端 未结 5 1835
日久生厌
日久生厌 2020-12-13 19:11

I\'m battling with Android\'s awful layout system. I\'m trying to get a table to fill the screen (simple right?) but it\'s ridiculously hard.

I got it to work someho

相关标签:
5条回答
  • 2020-12-13 19:23

    Finally worked out how to do this. Gave up on TableLayout and just used horizontal LinearLayouts inside a vertical one. The critical key is to set the weight. If you specify FILL_PARENT but with the default weight, it doesn't work:

    LinearLayout buttonsView = new LinearLayout(this);
    buttonsView.setOrientation(LinearLayout.VERTICAL);
    for (int r = 0; r < 6; ++r)
    {
        LinearLayout row = new LinearLayout(this);
        row.setOrientation(LinearLayout.HORIZONTAL);
        for (int c = 0; c < 4; ++c)
        {
            Button btn = new Button(this);
            btn.setText("A");
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
            lp.weight = 1.0f;
            row.addView(btn, lp);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        lp.weight = 1.0f;
        buttonsView.addView(row, lp);
    }  
    
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    setContentView(buttonsView, lp);
    
    0 讨论(0)
  • 2020-12-13 19:23

    Found the answer: apparently it is the layout_weight that makes it work and there is no way to set it from Java. Damnit.

    See How can I get an Android TableLayout to fill the parent in landscape mode?

    0 讨论(0)
  • 2020-12-13 19:24

    There are two mistakes in the above discussion.

    1. It is possible to programatically set the weight by specifying TableLayout.LayoutParams and TableRow.LayoutParams and using the appropriate constructor, e.g.

      TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
      
    2. A widget must have the LayoutParams of its parent. Therefore, the rows must use TableLayout.LayoutParams.

    This gives you the following working version of your initial code:

    TableLayout table = new TableLayout(this);
    // Java. You succeed!
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT);
    table.setLayoutParams(lp);
    table.setStretchAllColumns(true);
    
    TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            1.0f);
    TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            1.0f);
    for (int r = 0; r < 2; ++r)
    {
        TableRow row = new TableRow(this);
        for (int c = 0; c < 2; ++c)
        {
            Button btn = new Button(this);
            btn.setText("A");
            row.addView(btn, cellLp);
        }
        table.addView(row, rowLp);
    }
    setContentView(table);
    

    Thanks to Romain Guy's comment on Android developer's forum for the solution.

    0 讨论(0)
  • 2020-12-13 19:33

    To set TableLayout LayoutParams, we logically expect to use TableLayout.LayoutParams class, but you will get a cast error stating that TableLayout.LayoutParams cannot be casted into FrameLayout.LayoutParams.

    So, you should use FrameLayout.LayoutParams, if you want to programmatically set TableLayout properties. For example:

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
                layoutParams.setMargins(80, 0, 0, 0);
                TableLayout tableLayout = (TableLayout) findViewById(R.id.header_detail);
                tableLayout.setLayoutParams(layoutParams);
    
    0 讨论(0)
  • 2020-12-13 19:36

    You never set the row or button layout parameters whereas in the posted xml you do that..change the details of the for loops to set both the row layout parameters and the button layout parameters than it should give the same result as your xml.

    0 讨论(0)
提交回复
热议问题