答案 0 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fillable_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="@+id/table_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TableLayout
android:id="@+id/scrollable_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
package com.example.freez_t00;
public class MainActivity extends Activity {
private TextView recyclableTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
int[] fixedColumnWidths = new int[]{20, 20,20, 20, 20};
int[] scrollableColumnWidths = new int[]{20, 20, 20, 20, 20};
int fixedRowHeight = 70;
int fixedHeaderHeight = 70;
TableRow row = new TableRow(this);
//header (fixed vertically)
TableLayout header=(TableLayout)findViewById(R.id.table_header);
row.setLayoutParams(wrapWrapTableRowParams);
row.setBackgroundColor(Color.YELLOW);
row.addView(makeTableRowWithText("col1", fixedColumnWidths[0],fixedHeaderHeight));
row.addView(makeTableRowWithText("col2", fixedColumnWidths[1],fixedHeaderHeight));
row.addView(makeTableRowWithText("col3", fixedColumnWidths[2],fixedHeaderHeight));
row.addView(makeTableRowWithText("col4", fixedColumnWidths[3],fixedHeaderHeight));
row.addView(makeTableRowWithText("col5", fixedColumnWidths[4],fixedHeaderHeight));
header.addView(row);
// TableLayout fixedColumn = (TableLayout) findViewById(R.id.fixed_column);
//rest of the table (within a scroll view)
TableLayout scrollablePart = (TableLayout)findViewById(R.id.scrollable_part);
for(int i = 0; i < 10; i++) {
row = new TableRow(this);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.WHITE);
row.addView(makeTableRowWithText("value 1", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("value 2", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("value 3", scrollableColumnWidths[2], fixedRowHeight));
row.addView(makeTableRowWithText("value 4", scrollableColumnWidths[3], fixedRowHeight));
row.addView(makeTableRowWithText("value 5", scrollableColumnWidths[4], fixedRowHeight));
scrollablePart.addView(row);
}
}
public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
recyclableTextView = new TextView(MainActivity.this);
recyclableTextView.setText(text);
recyclableTextView.setTextColor(Color.BLACK);
recyclableTextView.setTextSize(20);
recyclableTextView.setGravity(Gravity.CENTER);
recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
recyclableTextView.setHeight(fixedHeightInPixels);
return recyclableTextView;
}
}
这可能对您有所帮助。 我只是改革你的代码。