1

このテーブルを可能な限り拡張して、それを含む線形レイアウトの残りの(空の)領域を埋めたい

ここに画像の説明を入力してください

次の方法でテーブルを生成します

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);


    }

コードの問題を正確に把握できませんでした。

編集 :

スクロールビューではなく線形レイアウトにテーブルを直接追加すると、テーブルは線形レイアウトの幅に収まります。私の問題はスクロールビューにあるようです

4

2 に答える 2

1

ここで答えを見つけました (Felixに感謝します)

スクロールビューに追加するコードは1行だけ

scrollView.setFillViewPort(true);
于 2012-06-30T12:01:56.003 に答える
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

于 2012-06-27T15:13:47.250 に答える