Reputation: 137
I have this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner_strassenfuehrer_fb_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:id="@+id/header_strassenfuehrer_screen_fb"
android:layout_span="8"
android:background="#A6A6A6"
android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
android:layout_span="8"
android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
And this part of code:
private TableLayout tableLayout;
private TableLayout tableHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fehlerdatenbank_strassenfuehrer_screen);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//determine height and width
int width = metrics.widthPixels;
//read database and put values dynamically into table
tableLayout = (TableLayout) findViewById(R.id.tablelayout_strassenfuehrer_fb_screen);
tableHeader = (TableLayout) findViewById(R.id.header_strassenfuehrer_screen_fb);
//initialize header row and define LayoutParams
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
TableRow header_row = new TableRow(this);
//column 1
TextView header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Störcode");
header_row.addView(header_tv);
//column 2
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Bezeichnung");
header_row.addView(header_tv);
//column 3
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Letztes Auftreten");
header_row.addView(header_tv);
//column 4
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Anlagen ID");
header_row.addView(header_tv);
//column 5
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Projekt");
header_row.addView(header_tv);
//column 6
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Benötigte Ersatzteile");
header_row.addView(header_tv);
//column 7
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Benötigtes Werkzeug");
header_row.addView(header_tv);
//column 8
header_tv = new TextView(this);
header_tv.setLayoutParams(params);
header_tv.setText("Handlungsleitfaden");
header_row.addView(header_tv);
tableHeader.addView(header_row, 0);
for (int i = 0; i < 100; i++) {
TableRow row = new TableRow(this);
//column 1
TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 2
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 3
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 4
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 5
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 6
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 7
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST");
row.addView(tv);
//column 8
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("TEST " + i);
row.addView(tv);
tableLayout.addView(row, i);
}
}
I tried to determine and set the columns of my rows equal.
Does anybody know, how I can determine the width correctly that the header_row
and the rows
under the header_row
will be in the same line?
I am new in android and also read some documentations, but could not find a solution yet. :(
Thanks!
EDIT: I updated the code, but it still looks not good with the header row and the rows. I would like, that all values are among each other. Thanks! :)
Upvotes: 1
Views: 1468
Reputation: 24211
You need to use android:stretchColumns="*"
in your TableLayout
declaration to get your columns to have equal width. So, please modify the layout as follows.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner_strassenfuehrer_fb_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:id="@+id/header_strassenfuehrer_screen_fb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_span="8"
android:background="#A6A6A6"
android:stretchColumns="*"></TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="8"
android:stretchColumns="*" />
</ScrollView>
</LinearLayout>
Hope that helps!
Upvotes: 1