问题描述
|
我在
ScrollView
里面有一个TableLayout
,也就是说我有一个可滚动的TableLayout
!当我在Activity
的不同位置创建Dialog
\时,它会动态填充。
实际上,一切正常,但事实是,在每种情况下,该表上都有一个不同的标题(每一列都有标题),并且它随行一起滚动。由于该表的内容是动态的,因此列的数量(以及宽度)是未知的。
因此,基本上,这就是我的问题。我看不到发布代码的要点,因为我主要需要一个建议/解决方法,但是如果这有助于克服问题,请告诉我。非常感谢一些样品。
解决方法
这是一种方法:
http://sdroid.blogspot.com/2011/01/fixed-header-in-tablelayout.html
请注意,有一种方法可以每次都进行这项工作:您要做的是在标题中创建一个类似的虚拟行,并将两个虚拟变量中的文本设置为您可以在该行中找到的最长字符串(以字符数表示) ,或者甚至更好的方法是与Paint.getTextWidths进行比较)
,
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>
,对我来说,我不想经历担心列对齐的麻烦,所以我首先创建了这个:
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是我视图中TableLayout的实例。这对我有用。
当然,您必须在开始创建表时将SET_TABLE_HEADER初始化为true。
,具有动态固定标头的Android可滚动TableLayout非常简单。遵循这个步骤
步骤1:
创建一个TableLayout并使用android:layout_alignParentTop = \“ true \”这将使此布局始终位于顶部。
第2步:
在ScrollView中使用,为没有行的内容创建另一个TableLayout(对于动态内容)。
第三步:
动态添加行和列。在添加每列(TextView)时,将宽度分配给一个临时变量。
防爆
tvContentText.measure(0,0);
tempmaxwidth = tvContentText.getMeasuredWidth();
步骤4:
检查最大宽度并将最大宽度分配给主变量。
例如:
如果(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);
}