带有动态固定标题的Android可滚动表格布局

带有动态固定标题的Android可滚动表格布局,android,Android,我在滚动视图中有一个表格布局,也就是说,我有一个可滚动的表格布局!当我在活动的不同位置创建对话框时,它会动态填充 实际上,一切都很好,但事实上,对于每种情况,表上都有一个不同的标题(每列都有一个标题),它会随着行一起滚动。由于该表的内容是动态的,因此列的数量(以及宽度)是未知的 所以,基本上,这是我的问题。我不认为发布代码有什么意义,因为我主要需要一个建议/解决方法,但如果这有助于克服这个问题,请告诉我。非常感谢您提供一些样品。这里有一个方法: 请注意,每次都有这样一种方法:您需要做的是在标题

我在
滚动视图中有一个
表格布局
,也就是说,我有一个可滚动的
表格布局
!当我在
活动
的不同位置创建
对话框
时,它会动态填充

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

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

这里有一个方法:

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

package com.test.test;
导入android.app.Activity;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.widget.TableLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类TestProjectTestActivity扩展活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl_head1=(TableLayout)findViewById(R.id.tl_head);
TableLayout tl_child 1=(TableLayout)findViewById(R.id.tl_child);
字符串头1=“”;
TableRow tr[]=新的TableRow[40];
TextView tv[]=新的TextView[1000];

对于(int i=0;i对于我来说,我不想麻烦地担心柱的对齐,所以我首先创建了以下内容:

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是我的表格布局的实例。这对我很有用


当然,您必须在开始创建表格时初始化将表格标题设置为true。

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

第1步:

创建一个TableLayout并使用android:layout\u alignParentTop=“true”
这将使该布局始终位于顶部

第二步:

使用in-ScrollView为没有行的内容创建另一个TableLayout(用于动态内容)

第三步:

动态添加行和列。在添加每列(TextView)时,将宽度指定给临时变量

Ex

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

第4步: 检查最大宽度并将最大宽度指定给主变量

Ex:

如果(tempmaxwidth>maxwidth) maxwidth=tempmaxwidth

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

Ex:

TextView tvh1=(TextView)findViewById(R.id.h1); tvh1.设置宽度(最大宽度)

下面是xml和代码

activity_main.xml

MainActivity.xml
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tl=(TableLayout)findviewbyd(R.id.contenttbl);
int tempmaxwidth=0,maxwidth=0;
int tempmaxwidth1=0,maxwidth1=0;
int tempmaxwidth2=0,maxwidth2=0;
int tempmaxwidth3=0,maxwidth3=0;
对于(int i=0;imaxwidth)
maxwidth=tempmaxwidth;
TextView tvContentText1=新的TextView(getApplicationContext());
//tvContentText1.setText(整数.toString(最大宽度));
tvContentText1.setText(“内容2”);
tvContentText1.setTextColor(Color.BLACK);
tvContentText1.setTextSize(16f);
tvContentText1.度量(0,0);
tempmaxwidth1=tvContentText1.getMeasuredWidth();
如果(tempmaxwidth1>maxwidth1)
maxwidth1=tempmaxwidth1;
TextView tvContentText2=新的TextView(getApplicationContext());
tvContentText2.setText(“内容3”);
tvContentText2.setTextColor(Color.BLACK);
tvContentText2.setTextSize(16f);
tvContentText2.度量(0,0);
tempmaxwidth2=tvContentText2.getMeasuredWidth();
如果(tempmaxwidth2>maxwidth2)
maxwidth2=tempmaxwidth2;
TextView tvContentText3=新的TextView(getApplicationContext());
tvContentText3.setText(“内容4”);
tvContentText3.setTextColor(Color.BLACK);
tvContentText3.setTextSize(16f);
tvContentText3.度量(0,0);
tempmaxwidth3=tvContentText3.getMeasuredWidth();
如果(tempmaxwidth3>maxwidth3)
maxwidth3=tempmaxwidth3;
addView(tvContentText);
newRow.addView(tvContentText1);
newRow.addView(tvContentText2);
newRow.addView(tvContentText3);
tl.addView(纽罗);
}
//为标题列指定最大宽度
TextView tvh1=(TextView)findViewById(R.id.h1);
tvh1.设置宽度(最大宽度);
TextView tvh2=(TextView)findViewById(R.id.h2);
tvh2.setWidth(maxwidth1);
TextView tvh3=(TextView)findViewById(R.id.h3);
tvh3.设置宽度(最大宽度2);
TextView tvh4=(TextView)findViewById(R.id.h4);
tvh4.设置宽度(最大宽度3);
}

您可以使用两个单独的表,一个只包含标题数据,另一个包含内容,
if (SET_TABLE_HEADER) {
    setTableHeader(tbl);
}
<?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>
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);
}