【问题标题】:Table doesn't fill the containing layout's width表格未填充包含布局的宽度
【发布时间】:2012-06-30 20:01:56
【问题描述】:

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

我使用以下方法生成表格

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


    }

我无法准确找出我的代码存在的问题。

编辑:

当我将表格直接添加到线性布局而不是滚动视图时,表格适合线性布局宽度。看来我的问题在于滚动视图

【问题讨论】:

  • 试试 tableLayout.setStretchAllColumns(true)..
  • @Dheeresh 我试过了。还是一样的问题
  • 这行是否是您的问题:“table.addView(headerRow, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));”而是将该宽度也更改为 fill_parent?
  • @Cruceo 我全部更改为 FILL_PARENT。没有新结果。

标签: android android-layout


【解决方案1】:

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

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

scrollView.setFillViewPort(true);

【讨论】:

    【解决方案2】:

    在你的 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);
    

    问候, 阿奇夫

    【讨论】:

    • 检查我的编辑!为要添加到 TableRows 或 Headers 的每个 TextVIew 添加它。编辑:该权重参数将是在行项目之间平等划分屏幕的关键。
    • 我将代码中的所有布局参数更改为 FILL_PARENT 并添加了您的代码,但根本没有任何更改。桌子的外观还是一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2015-05-27
    • 2010-11-30
    • 2017-04-09
    • 2011-05-02
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多