【问题标题】:android Table Layout does not fit the screenandroid表格布局不适合屏幕
【发布时间】:2015-07-21 12:29:42
【问题描述】:

我正在使用带有垂直滚动视图的表格布局以编程方式显示大量数据列表。在平板电脑上它以完美的方式工作,但是当我在 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++;
        }

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    使用 LinearLayout 而不是 RelativeLayout 它可能会解决您的问题。因为RelativeLayout相互重叠

    【讨论】:

    • 照做了
    猜你喜欢
    • 2013-04-18
    • 2013-02-05
    • 2017-07-05
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多