Android Tablelayout Kopfzeile
-
18-09-2019 - |
Frage
Okay, so habe ich das Tablelayout bekam, und es ist voller Daten - mit allen Zeilen Programm hinzugefügt. Ich habe innerhalb eines HorizontalScrollView das Tablelayout bekam, was wiederum in einem Scrollview ist - das gibt mir sowohl horizontal als auch vertikal scrollen. Was ich versuche jetzt zu tun ist, eine Kopfzeile, um es hinzuzufügen, die nicht bewegen wird. Ich habe zu bewegen Dinge um versucht so, dass meine beiden Scroll-Ansichten tatsächlich waren innerhalb des Tablelayout und das Hinzufügen der TableRows zum HorizontalScrollView; Meine Hoffnung war in der Lage sein, um dann außerhalb der Scroll-Ansichten, die die Kopfzeile hinzuzufügen.
Das einzige, was ich denken kann, ist eine zweite Tabelle Layout nur für die Kopfzeile zu haben, aber die Spalten immer scheint zu aufreihen wie es schwierig sein würde. Irgendwelche Ideen?
Lösung
Ein Ansatz ist durch ein Tablelayout innerhalb eines anderen Tablelayouts der Reihe Einbetten und die Header in der vorhergehenden Reihe setzen, wie unten gesehen. Ausrichten der Daten und der Header erfordert das layout_width Eigentum der Kopf Objekte anzeigen und nach Ansicht der Datenobjekte auf die gleichen dip Werte eingestellt werden. Auch die layout_weight Eigenschaft der Ansicht Objekte Innentablelayout muss seinen entsprechenden Kopf übereinstimmen. Jetzt, in der XML unten, ich habe 3 Textviews im inneren Tablelayout in einer einzigen Reihe angeordnet sind mit den Spaltenüberschriften entsprechen. Dies ist nur zu zeigen, wie die Ausrichtung durchgeführt werden kann. Sie können diese Daten füllen programmatisch durch ein Layout Aufblasen und zur Laufzeit hinzugefügt wird.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Name"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1"/>
<TextView android:text="Score"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="Level"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
<ScrollView android:layout_height="120dp">
<TableLayout android:id="@+id/score_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Al"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1">
</TextView>
<TextView android:text="1000"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="2"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</TableLayout>
Andere Tipps
Ich kam tatsächlich mit einem anderen anständigen Weg, dies zu tun.
einfach den Tisch bauen normalerweise mit der Kopfzeile als erste Zeile, innerhalb einer vertikalen Ausrichtung Linearlayout. Als nächstes entfernt programmatisch die erste Zeile dann als mit dem Linearlayout erstes Kind hinzufügen. Das funktionierte wie ein Charme.
Edit: Dies funktioniert auch ohne statische Spaltenbreiten angeben zu müssen
.Ich weiß, dass die Frage ist alt, aber es war das erste, dass Google hat mir, wie ich das gleiche Problem gehabt haben. Und da ich glaube, ich habe eine bessere Lösung gefunden, ich mag es teilen.
Idee:. Das Tablelayout setzen (im Scroll) in RelativeLayout und ein Overlay erstellen, dass die erste (Header) der Reihe über alles andere ziehen würde
Hier ist layout.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:id="@+id/table_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="UselessParent"
>
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
</RelativeLayout>
Und hier ist der Code:
TableLayout table = (TableLayout)view.findViewById(R.id.table);
final TableRow headerRow = new TableRow(context);
table.addView(headerRow);
table.addView(new TableRow(context));
table.addView(new TableRow(context));
table.addView(new TableRow(context));
RelativeLayout tableWrapper = (RelativeLayout)view.findViewById(R.id.table_wrapper);
View fakeHeaderView = new View(context) {
@Override
public void draw(Canvas canvas) {
headerRow.draw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = headerRow.getMeasuredWidth();
int height = headerRow.getMeasuredHeight();
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
};
tableWrapper.addView(fakeHeaderView);
Für diejenigen, die nicht zufrieden mit mit Ihren Größen vordefinieren, fand ich ein bisschen wie ein Hack, die für mich arbeiten wird.
Im Grunde machen eine separate Tabelle für den Titel und legt es über die Haupttabelle, aber mit der gleichen Top Ausrichtung, dann zwei Kopien der Titelzeile erstellen und nach einem zu der Haupttabelle hinzufügen, fügen Sie die andere auf den Titel Tisch und das Kind nach Ansicht des layoutParams zu der Reihe bildet die Haupttabelle festgelegt.
Hier ist mein einfaches Beispiel.
in Ihrem Layout:
<HorizontalScrollView
android:id="@+id/table_horizontal_scroll_view"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:id="@+id/table_vertical_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grid_table_layout"
/>
</ScrollView>
<TableLayout
android:layout_alignLeft="@+id/table_vertical_scroll_view"
android:layout_alignRight="@+id/table_vertical_scroll_view"
android:layout_alignStart="@+id/table_vertical_scroll_view"
android:layout_alignEnd="@+id/table_vertical_scroll_view"
android:layout_alignParentTop="true"
android:background="@color/grid_view_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/grid_floating_row_layout"
/>
</RelativeLayout>
</HorizontalScrollView>
Dann, wenn Sie Ihre Zeilen hinzufügen:
//clear out any views
tableLayout.removeAllViews();
floatingRowLayout.removeAllViews();
TableRow[] rows = getTableContentRows() // content of your table
TableRow[] titleRows = {getTitleRow(), getTitleRow()}; //two copies of your title row
tableLayout.addView(titleRows[0]); // first add the first title to the main table
addRows(rows) // add any other rows
floatingRowLayout.addView(titleRows[1]); // floatingRowLayout is connected to id/grid_floating_row_layout
titleRows[0].setVisibility(View.INVISIBLE); // make the title row added to the main table invisible
// Set the layoutParams of the two title rows equal to each other.
// Since this is done after the first is added to the main table, they should be the correct sizes.
for(int i = 0; i < titleRows[0].getChildCount(); i++) {
titleRows[1].getChildAt(i).setLayoutParams(titleRows[0].getChildAt(i).getLayoutParams());
}
Verwenden von zwei Tablelayouts One in Scrollview wie give android:stretchColumns="*"
<RelativeLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingTop="5dp"
android:layout_height="match_parent"
android:layout_below="@+id/llSpinner">
<TableLayout
android:layout_width="match_parent"
android:id="@+id/tableHead"
android:stretchColumns="*"
android:layout_height="wrap_content">
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tableTotal"
android:layout_below="@+id/tableHead"
android:id="@+id/scrolltable">
</ScrollView>
<TableLayout
android:layout_width="match_parent"
android:id="@+id/tableTotal"
android:stretchColumns="*"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
</TableLayout>
</RelativeLayout>
erstellen Sie dann eine gemeinsame Sicht für die Zeilen und wichtigste Erwähnung android:layout_width="0dp"
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true"
android:focusableInTouchMode="false"
android:clickable="true"
android:background="@android:drawable/list_selector_background">
<TextView
android:text="S.No"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/tbsNo"
android:layout_weight="1"
android:gravity="center"
android:layout_column="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Date"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tbDate"
android:layout_column="2" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Count"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tbMrCount"
android:layout_column="3" />
</TableRow>
Jetzt in Aktivität
Tablelayout tableHeading =(TableLayout)findViewById(R.id.tableHead);
Tablelayout table =(TableLayout) findViewById(R.id.table_repots);
trHeading = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null);
trHeading.setBackgroundColor(Color.parseColor("#6688AC"));
trHeading.setPadding(0,10,0,10);
TextView tv;
tv = (TextView) trHeading.findViewById(R.id.tbsNo);
tv.setText("S.No");
tv = (TextView) trHeading.findViewById(R.id.tbDate);
tv.setText("Date");
table.addView(tv);