Спасибо @airfish и @Onik за ваши решения. Решение от @airfish помогло решить мою проблему, после двух дней борьбы с ней.
Вот окончательное решение для всех, кто заинтересован в том, чтобы заставить это работать.
Файл окончательного макета.
<?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">
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="45dp">
<TableLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_marginTop="-45dp"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
MainActivity.java (Изменить строку # 30 для увеличения / уменьшения количества строк для тестирования)
package com.example.scroll_ex;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout mainTable = findViewById(R.id.mainTable);
mainTable.removeAllViews();
int leftRowMargin = 0;
int topRowMargin = 0;
int rightRowMargin = 0;
int bottomRowMargin = 0;
int textSize = 0, smallTextSize = 0, mediumTextSize = 0;
TableRow tr = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
for(int i=-1; i < 25; i++){
tr = new TableRow(this);
column1 = new TextView(this);
column2 = new TextView(this);
tr.setGravity(Gravity.CENTER);
tr.setId(i+ 1);
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
tr.setPadding(0,0,0,0);
tr.setLayoutParams(trParams);
if (i == -1) {
column1.setText("Name");
column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
column1.setPadding(5, 5, 1, 5);
} else {
column1.setText("Name #" + i);
column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
column1.setPadding(5, 0, 1, 5);
}
tr.addView(column1);
if (i == -1) {
column2.setText("Address");
column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
column2.setPadding(5, 5, 1, 5);
} else {
column2.setText("Address " + i);
column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
column2.setPadding(5, 0, 1, 5);
}
tr.addView(column2);
mainTable.addView(tr, trParams);
}
}
}