表未填充包含布局的宽度

时间:2012-06-27 15:07:27

标签: android android-layout

我希望此表尽可能扩展以填充包含它的线性布局的剩余(空)区域

enter image description here

我使用以下方法生成表

public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){

        //clearing previous views
         tableLayout.setVisibility(View.VISIBLE);
         tableLayout.setGravity(Gravity.CENTER);
         int chids = tableLayout.getChildCount();
         if (chids > 0){
             tableLayout.removeAllViews();
         }

        if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
            return;
        }

        TableLayout table = new TableLayout(context);  
//      table.setStretchAllColumns(true);  
//      table.setShrinkAllColumns(true);  


        //Rendering the table title
        TextView tableTitleView = new TextView(context);
        tableTitleView.setText(tableTitle);
        tableTitleView.setTextColor(Color.WHITE);
        tableTitleView.setGravity(Gravity.CENTER);
        tableTitleView.setTextSize(18);
        tableTitleView.setTextAppearance(context, R.style.boldText);
        tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
        tableTitleView.setPadding(2, 2, 2, 10);
        tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));








        ScrollView scroll1 = new ScrollView(context);
        HorizontalScrollView scroll2 = new HorizontalScrollView(context);


        int rows = data.length;
        int columns = data[0].length;

        if(headerTitles!=null){
            //Rendering the header
            TableRow headerRow = new TableRow(context);
            for (int i = 0; i < columns; i++) {
                TextView t = new TextView(context);
                t.setTextSize(17);
                t.setTextAppearance(context, R.style.boldText);
                t.setPadding(5, 5, 5, 5);
                t.setText(headerTitles[i]);
                t.setGravity(Gravity.CENTER);
                t.setTextColor(Color.WHITE);
                t.setBackgroundResource(R.drawable.text_border_header_1);
                headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
            }
            table.addView(headerRow, new TableLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }

        //Rendering the Table Data
        TableRow row;
        for (int current = 0; current < rows ; current++) {
            row = new TableRow(context);
            TextView t;
            for(int i =0 ; i < columns ; i++){
                t = new TextView(context);
                t.setTextSize(15);
                t.setPadding(5,5,5,5);
                t.setText(data[current][i]);
                t.setGravity(Gravity.CENTER);

                if (type == 1) {
                    if (current % 2 == 0) {
                        t.setBackgroundResource(R.drawable.text_border_odd);
                    } else {
                        t.setBackgroundResource(R.drawable.text_border_even);
                    }
                }else if (type == 2) {
                    if (current % 2 == 0) {
                        t.setBackgroundResource(R.drawable.text_border_odd_2);
                    } else {
                        t.setBackgroundResource(R.drawable.text_border_even_2);
                    }
                }else if (type == 3) {
                    if (current % 2 == 0) {
                        t.setBackgroundResource(R.drawable.text_border_odd_3);
                    } else {
                        t.setBackgroundResource(R.drawable.text_border_even_3);
                    }
                }

                t.setTextColor(Color.BLACK);
                row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
            }

            table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        }
        scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        scroll1.setPadding(2, 20, 2, 20);

        tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);


    }

我无法弄清楚我的代码究竟是什么问题。

编辑:

当我将表格直接添加到线性布局而不是滚动视图时,表格符合线性布局宽度。我的问题似乎是滚动视图

2 个答案:

答案 0 :(得分:1)

我找到了答案Here(感谢Felix)

只需将一行代码添加到滚动视图

scrollView.setFillViewPort(true);

答案 1 :(得分:0)

在你的TableLayout对象上试试这个。

TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);

编辑:

TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);

的问候, Aqif