这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MainLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MainTable"
android:layout_marginTop="5dp">
</TableLayout>
</ScrollView>
</RelativeLayout>
这是我的代码
private void showMarkets () {
markets = marketAdapter.getAllMarkets();
TableRow newRow = new TableRow(getApplicationContext());
//TableRow newRow = new TableRow(this);
newRow.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
newRow.setGravity(Gravity.RIGHT);
for (int i = 0; i < marketAdapter.headingTitle.length; i++){
newRow.addView(createView(marketAdapter.headingTitle[i], 20, android.R.color.white));
Log.e("title", marketAdapter.headingTitle[i]);
}
table.addView(newRow);
}
public TextView createView (String columnName, int size, int columnColor)
{
TextView textView = new TextView(getApplicationContext());
textView.setText(columnName);
textView.setTextSize(size);
textView.setTextColor(getResources().getColor(columnColor));
textView.setPadding(5, 10, 5, 10);
textView.setGravity(Gravity.CENTER);
return textView;
}
添加我的创建视图功能
答案 0 :(得分:0)
ScrollView应该是父元素。您不需要相对布局。试试这个:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MainTable"
android:layout_marginTop="5dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView in a row"/>
</TableRow>
</TableLayout>
答案 1 :(得分:0)
您需要将布局参数设置为新行:
private void showMarkets () {
markets = marketAdapter.getAllMarkets();
TableRow newRow = new TableRow(getApplicationContext());
//TableRow newRow = new TableRow(this);
newRow.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
newRow.setGravity(Gravity.RIGHT);
for (int i = 0; i < marketAdapter.headingTitle.length; i++){
//add layout params to view inside layout row as well
newRow.addView(createView(marketAdapter.headingTitle[i], 20, android.R.color.white),new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)));
Log.e("title", marketAdapter.headingTitle[i]);
}
newRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
table.addView(newRow);
}