简体   繁体   中英

android Table Layout does not fit the screen

I'm using a table layout with a vertical scroll view to display a huge list of Data programmaticlly. On a tablet it works with a perfect way but the problem when I run it on a phone like galaxy or nexus 1 the width of the table layout does not match the width of the screen so two or three columns are hidden I have tried all combination match_parent , wrap_content ... I used an horizontal scroll view it works but I don't think that it's a good solution this is the xml file :

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

this is my code :

  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++;
        }

use LinearLayout instead of RelativeLayout it may solve your problem. because RelativeLayout overlaps one on other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM