android表格布局不适合屏幕

时间:2015-07-21 12:29:42

标签: android android-tablelayout

我正在使用带有垂直滚动视图的表格布局,以编程方式显示大量数据。在平板电脑上它以完美的方式工作,但问题是我在像galaxy或nexus 1这样的手机上运行时表格布局的宽度与屏幕的宽度不匹配所以隐藏了两三列我尝试了所有组合match_parent,wrap_content ...我使用了水平滚动视图它可以工作,但我不认为这是一个很好的解决方案 这是xml文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="..."
    >
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mv_table"  
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_marginTop="50dp">
    </TableLayout>
</RelativeLayout>
</ScrollView>

这是我的代码:

  TableLayout mvtable = (TableLayout) findViewById(R.id.mv_table);
        TableRow tr_head = new TableRow(this);
        tr_head.setId(10);
        tr_head.setBackgroundColor(Color.rgb(24,65,95));
        TextView label_Date = new TextView(this);
        label_Date.setId(20);
        label_Date.setText("Date");
        label_Date.setTextColor(Color.WHITE);
        label_Date.setPadding(5, 5, 5, 5);
        tr_head.addView(label_Date);

        TextView label_Debit = new TextView(this);
        label_Debit.setId(21);
        label_Debit.setText("Débit");
        label_Debit.setTextColor(Color.WHITE); 
        label_Debit.setPadding(5, 5, 5, 5); 
        tr_head.addView(label_Debit); 

.....

  tr_head.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
        mvtable.addView(tr_head, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));

        //setting data programmaticlly 
        int c ;
        int i=1;

        for (Movement mv : movementsList)
        {

            TableRow tr = new TableRow(this);
            if (i % 2 != 0)
            {
               c=Color.WHITE;

                tr.setBackgroundColor(Color.GRAY);}
            else
            {c=Color.BLACK;
            }
            tr.setId(100 + i);
            tr.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));



            TextView labelDate = new TextView(this);
            labelDate.setId(200+i);
            labelDate.setText(mv.getDate());
            labelDate.setTextColor(c);
            labelDate.setPadding(5, 5, 5, 5);
            tr.addView(labelDate);


            TextView labelDebit = new TextView(this);
            labelDebit.setId(210+i);
            labelDebit.setText(mv.getDebit());
            labelDebit.setTextColor(c);
            labelDebit.setPadding(5, 5, 5, 5); 
             labelDebit.setBackgroundColor(Color.rgb(255,174,185));
            tr.addView(labelDebit); 

......



            tr.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));

            mvtable.addView(tr, new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            i++;
        }

1 个答案:

答案 0 :(得分:0)

使用LinearLayout而不是RelativeLayout它可以解决您的问题。因为RelativeLayout与其他

重叠