Setting a semi fill_parent layout width in an Android interface
I am trying to create a table row element where the first line contains a title followed by an ImageView that should 开发者_运维问答be all of the way to the right. The TextView should fill the entire width of the phone except for the ImageView at the end.
The solution depends on some details of the circumstances.
If all the rows have the same format
If all the rows have the same column layout, then set android:stretchColumns="0"
on the TableLayout. This will make the first column (index 0) stretch to fill any remaining space.
If the header has a different format from the remaining rows
If you have more columns or need a different column layout for the rest of the rows, then you need to do something different. I don't believe any single item can span multiple columns.
If only the positioning is important and you don't really need to span across multiple rows, you can use the android:layout_column
attribute on each of the header items. The column number is 0-based, so the first column is 0. The Eclipse layout builder doesn't seem to present this attribute, but it will handle the attribute if you type it into the xml.
If you can't fit your elements inside the same columns the rest of the table uses, then the header doesn't belong in a TableRow. As suggested in another answer, you can use a RelativeLayout instead of the TableRow. Alternately, you could move the header outside of the table.
First, use a RelativeLayout for your row. Use the typical ImageView, give it an id, and use the XML attribute android:layout_alignParentRight="true"
. Next, use the TextView, use android:layout_toLeftOf="@id/myid"
and make sure it's width is "fill_parent"
.
edit: code tags
精彩评论