【问题标题】:Android scrollable TableLayout with a dynamic fixed header具有动态固定标题的 Android 可滚动 TableLayout
【发布时间】:2011-04-14 15:35:57
【问题描述】:

我在ScrollView 中有一个TableLayout,也就是说我有一个可滚动的TableLayout!当我在Activity 的不同位置创建Dialog 时,它会动态填充。

实际上一切正常,但事实上,对于每种情况,该表上都有一个不同的标题(每列都有一个标题),并且它会随着行滚动。由于该表的内容是动态的,因此列的数量(以及宽度)是未知的。

所以,基本上,这是我的问题。我没有看到发布代码的意义,因为我主要需要一个建议/解决方法,但如果这有助于解决这个问题,请告诉我。非常感谢一些样品。

【问题讨论】:

  • 您可以使用两个单独的表,一个只包含标题数据,另一个包含内容,并且只在滚动视图中包含内容表。
  • 如何获取每个标题的尺寸?
  • 只将部分行放在滚动视图中会起作用,还是这些行需要是表的直接子级?
  • 不,不管这些是不是直系孩子。

标签: android


【解决方案1】:

这是一种方法: http://sdroid.blogspot.com/2011/01/fixed-header-in-tablelayout.html

请注意,有一种方法可以每次都进行这项工作:您要做的是在标题中创建一个类似的虚拟行,并将两个虚拟中的文本设置为您可以在该行中找到的最长字符串(在字符数,或者更好的是,与 Paint.getTextWidths 进行比较)

【讨论】:

  • 是的,确实,这似乎有效。即使我已经通过定义每列应分配的总宽度百分比来解决我的问题。但我会接受这篇文章,因为它似乎回答了这个问题。
【解决方案2】:
    package com.test.test;

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;

    public class TestProjectTestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TableLayout tl_head1 = (TableLayout)findViewById(R.id.tl_head);
            TableLayout tl_child1 = (TableLayout)findViewById(R.id.tl_child);

            String headcol1="";
            TableRow tr[]= new TableRow[40];
            TextView tv[] = new TextView[1000];
            for(int i=0; i <=30; i++)
            {

                tr[i]=new TableRow(this);


                for(int j=1; j<=5; j++)
                {
                    tv[j]=new TextView(this);
                    tv[j].setId(j);
                    tv[j].setText("testingmultiple data hello"+""+j);
                    tv[j].setTextColor(Color.WHITE);


                    if(headcol1.length() < tv[j].getText().length())
                    {
                        headcol1=null;
                        headcol1=tv[j].getText().toString();
                    }
                    tr[i].addView(tv[j]);
                }

                tl_child1.addView(tr[i]);
            }

            TableRow trhead= new TableRow(this);
            TextView tvhead[] = new TextView[5];

            for(int i=0;i<=4;i++)
            {
                tvhead[i] = new TextView(this);
                tvhead[i].setTextColor(Color.WHITE);
                tvhead[i].setHeight(0);
                tvhead[i].setText(headcol1);
                trhead.addView(tvhead[i]);

            }
            tl_head1.addView(trhead);


        }
    }



<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hsv_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/tl_head"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tr_head"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
                <TextView
                android:id="@+id/tv_hidden1"
                android:layout_gravity="center_horizontal"
                android:text="12"
                android:textColor="#FFFFFF"
                 />
                <TextView
                android:id="@+id/tv_hidden2"

                android:layout_gravity="center_horizontal"
                android:text="123"
                android:textColor="#FFFFFF"

                />
                <TextView
                android:id="@+id/tv_hidden3"

                android:layout_gravity="center_horizontal"
                android:text="1234"
                android:textColor="#FFFFFF"

                />
                <TextView
                android:id="@+id/tv_hidden4"

                android:layout_gravity="center_horizontal"
                android:text="12345"
                android:textColor="#FFFFFF"

                />
                <TextView
                android:id="@+id/tv_hidden5"

                android:layout_gravity="center_horizontal"
                android:text="123456"
                android:textColor="#FFFFFF"
                              />

            </TableRow>
        </TableLayout>

        <ScrollView
            android:id="@+id/sv_child"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TableLayout
                android:id="@+id/tl_child"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TableLayout>
        </ScrollView>
    </LinearLayout>
</HorizontalScrollView>

【讨论】:

  • 这种方法的问题是头部和主体列的宽度不会同步。
【解决方案3】:

带有动态固定标题的Android 可滚动TableLayout 非常简单。请按照以下步骤操作

第 1 步:

创建一个 TableLayout 并使用 android:layout_alignParentTop="true" 这将使该布局始终位于顶部。

第 2 步:

在 ScrollView 中为没有行的内容创建另一个 TableLayout(用于动态内容)。

第 3 步:

动态添加行和列。在添加每一列(TextView)时,将宽度分配给一个临时变量。

tvContentText.measure(0, 0); tempmaxwidth=tvContentText.getMeasuredWidth();

第 4 步: 检查最大宽度并将最大宽度分配给主变量。

例如:

if (tempmaxwidth > maxwidth) maxwidth=tempmaxwidth;

第 5 步: 分配内容详细信息后。将每列的最大宽度分配给其标题列。

例如:

TextView tvh1 = (TextView) findViewById(R.id.h1); tvh1.setWidth(maxwidth);

xml和代码如下

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <TableLayout
        android:id="@+id/headertbl"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:layout_height="wrap_content"
        android:stretchColumns="*">
        <TableRow>
            <TextView
                android:id="@+id/h1"
                android:layout_height="wrap_content"
                android:text="H1"
                android:textStyle="bold"
                android:textAlignment="center"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/h2"
                android:layout_height="wrap_content"
                android:text="H2"
                android:textStyle="bold"
                android:textAlignment="center"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/h3"
                android:layout_height="wrap_content"
                android:text="H3"
                android:textStyle="bold"
                android:textAlignment="center"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/h4"
                android:layout_height="wrap_content"
                android:text="H4"
                android:textStyle="bold"
                android:textAlignment="center"
                android:textSize="20sp" />
        </TableRow>
    </TableLayout>

    <ScrollView
        android:layout_below="@id/headertbl"
        android:id="@+id/container"
        android:layout_marginTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">
     <TableLayout
         android:id="@+id/contenttbl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*">
        </TableLayout>
    </ScrollView>
</RelativeLayout>

MainActivity.xml

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TableLayout tl = (TableLayout) findViewById(R.id.contenttbl);

    int tempmaxwidth=0,maxwidth=0;
    int tempmaxwidth1=0,maxwidth1=0;
    int tempmaxwidth2=0,maxwidth2=0;
    int tempmaxwidth3=0,maxwidth3=0;

    for(int i=0;i<50;i++)
    {
        TableRow newRow = new TableRow(getApplicationContext());
        newRow.setId(i);
        TextView tvContentText = new TextView(getApplicationContext());
        if(i!=6)
            tvContentText.setText("Content 1");
        else
            tvContentText.setText("---- Content with Max width");
            tvContentText.setTextColor(Color.BLACK);
            tvContentText.setTextSize(16f);
            tvContentText.measure(0, 0);
            tempmaxwidth=tvContentText.getMeasuredWidth();

        if (tempmaxwidth>maxwidth)
            maxwidth=tempmaxwidth;

        TextView tvContentText1 = new TextView(getApplicationContext());
        //tvContentText1.setText(Integer.toString(maxwidth));
        tvContentText1.setText("Content 2");
        tvContentText1.setTextColor(Color.BLACK);
        tvContentText1.setTextSize(16f);
        tvContentText1.measure(0, 0);
        tempmaxwidth1=tvContentText1.getMeasuredWidth();

        if (tempmaxwidth1>maxwidth1)
            maxwidth1=tempmaxwidth1;

        TextView tvContentText2 = new TextView(getApplicationContext());
        tvContentText2.setText("Content 3");
        tvContentText2.setTextColor(Color.BLACK);
        tvContentText2.setTextSize(16f);
        tvContentText2.measure(0, 0);
        tempmaxwidth2=tvContentText2.getMeasuredWidth();

        if (tempmaxwidth2>maxwidth2)
            maxwidth2=tempmaxwidth2;

        TextView tvContentText3 = new TextView(getApplicationContext());
        tvContentText3.setText("Content 4");
        tvContentText3.setTextColor(Color.BLACK);
        tvContentText3.setTextSize(16f);
        tvContentText3.measure(0, 0);
        tempmaxwidth3=tvContentText3.getMeasuredWidth();

        if (tempmaxwidth3>maxwidth3)
            maxwidth3=tempmaxwidth3;

        newRow.addView(tvContentText);
        newRow.addView(tvContentText1);
        newRow.addView(tvContentText2);
        newRow.addView(tvContentText3);
        tl.addView(newRow);
    }
    // Assigning Max width to header column
    TextView tvh1 = (TextView) findViewById(R.id.h1);
    tvh1.setWidth(maxwidth);

    TextView tvh2 = (TextView) findViewById(R.id.h2);
    tvh2.setWidth(maxwidth1);

    TextView tvh3= (TextView) findViewById(R.id.h3);
    tvh3.setWidth(maxwidth2);

    TextView tvh4= (TextView) findViewById(R.id.h4);
    tvh4.setWidth(maxwidth3);
}

【讨论】:

    【解决方案4】:

    对我来说,我不想担心列对齐的麻烦,所以我首先创建了这个:

    public static Boolean SET_TABLE_HEADER;
    

    然后我创建了这个函数来设置我的 TableHeader:

    public void setTableHeader(TableLayout tbl) {
        TableRow tr = new TableRow(context);
    
        TextView tcView = new TextView(context);
        tcView.setText("Class");
        tcView.setTextColor(Color.BLACK);
        tcView.setAllCaps(true);
        tcView.setTypeface(null, Typeface.BOLD);
        tcView.setGravity(Gravity.CENTER_HORIZONTAL);
        tr.addView(tcView);
    
        TextView tfView = new TextView(context);
        tfView.setText("Fee");
        tfView.setTextColor(Color.BLACK);
        tfView.setAllCaps(true);
        tfView.setTypeface(null, Typeface.BOLD);
        tfView.setGravity(Gravity.CENTER_HORIZONTAL);
        tr.addView(tfView);
    
        TextView tqView = new TextView(context);
        tqView.setText("Available");
        tqView.setTextColor(Color.BLACK);
        tqView.setAllCaps(true);
        tqView.setTypeface(null, Typeface.BOLD);
        tqView.setGravity(Gravity.CENTER_HORIZONTAL);
        tr.addView(tqView);
    
        // Adding row to Table
        tbl.addView(tr);
    
        // Table Header Has Been Set
        SET_TABLE_HEADER = false;
    }
    

    然后在我的循环中:

    if (SET_TABLE_HEADER) {
        setTableHeader(tbl);
    }
    

    在我看来,tbl 是我的 TableLayoutinstance。这对我有用。

    当然,您必须在开始创建表时将 SET_TABLE_HEADER 初始化为 true。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2017-10-03
      • 1970-01-01
      相关资源
      最近更新 更多