Order rows in TableLayout
I have TableLayout in my xml.
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/jobs_table"
android:gravity="top"
android:stretchColumns="*"
android:shrinkColumns="*"/>
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/monday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tuesday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wednesday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
开发者_如何转开发 android:text="@string/thursday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/friday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saturday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sunday"/>
</TableRow>
Then, I add rows with buttons in Activity.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jobs);
tableLayout = (TableLayout) findViewById(R.id.jobs_table);
// Make some buttons here.
TableRow row = new TableRow(this);
tableLayout.addView(row, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
When I run application, row with TextViews from XML are on the bottom and Buttons from Java code are on the top. How can I reverse them(Buttons are on the button and row with TextViews are on the top)?
I think this version of addView does what you need (with index = 0)
public void addView (View child, int index)
Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child. Parameters
child the child view to add
index the position at which to add the child
So, I did it as:
TextView[] days = new TextView[7];
for(int i = 0; i < 7; i++) {
days[i] = new TextView(context);
days[i].setGravity(Gravity.CENTER);
row.addView(days[i]);
}
days[0].setText(R.string.monday);
days[1].setText(R.string.tuesday);
days[2].setText(R.string.wednesday);
days[3].setText(R.string.thursday);
days[4].setText(R.string.friday);
days[5].setText(R.string.saturday);
days[6].setText(R.string.sunday);
rowsWithButtons.add(row);
精彩评论