I'm getting different results when creating a TableLayout from xml vs. programmatically. In pure xml, all three children of the TableRow (which are ImageView subclasses in my test code) expand in width so they fill the table horizontally, each taking one-third the width. But when done programmatically, they are always the minimum size they can be to hold their content. I want them to expand, but I don't want to hard code the xml.
Although I also tried "pure" programmatic building of the children of the TableRow, below I show it my preferred way, where I, in a loop, inflate each of the children from an xml file that contains just that component, and then add them to the TableRow. (note that I create the TableRow programmatically)
Here is main.xml. Note that the TableRow and children are commented out here, since this is the programmatic version. (uncomment to do it purely xml)
<view class="com.whatev.ButtonMenu"
android:background="@drawable/test1"
android:layout_height="50px" android:layout_width="fill_parent" android:padding="0px"
android:layout_alignBottom="@+id/map" android:id="@+id/buttonMenu" >
<!-- TableRow>
<view xmlns:android="http://schemas.android.com/apk/res/android" class="com.whatev.MenuButton"
android:background="@drawable/test2" android:src="@drawable/bg"
android:scaleType="center" android:cropToPadding="false" android:padding="3px" android:layout_weight="20"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
<view xmlns:android="http://schemas.android.com/apk/res/android" class="com.whatev.MenuButton"
android:background="@drawable/test2" android:src="@drawable/bg"
android:scaleType="center" android:cropToPadding="false" android:padding="3px" android:layout_weight="20"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
<view xmlns:android="http://schemas.android.com/apk/res/android" class="com.whatev.MenuButton"
android:background="@drawable/test2" android:src="@drawable/bg"
android:scaleType="center" android:cropToPadding="false" android:padding="3px" android:layout_weight="20"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
</TableRow -->
</view>
Then in filterbutton.xml, I have this (which is the same as the repeated stuff in comments above):
<?xml version="1.0" encoding="utf-8"?>
<view xmlns:android="http://schemas.android.com/apk/res/android" class="com.whatev.MenuButton"
android:background="@drawable/test2" android:src="@drawable/bg"
android:scaleType="center" android:cropToPadding="false" android:padding="3px" android:layout_weight="20"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
/>
Finally, here is the java code for the constructor of ButtonMenu, which is a subclass of TableLayout. (comment out everything but the call to super() to do it pure XML.)
public ButtonMenu(Context context, AttributeSet attrs) {
super(context, attrs);
TableRow firstTableRow = new TableRow(context);
for (int i=0; i<3; i++) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MenuButton mb = (MenuButton)inflater.inflate(R.layout.filterbutton, null);
firstTableRow.addView(mb);
}
this.addView(firstTableRow);
}
Any ideas why it behaves differently, or what I have to do to get the children ("MenuButtons") to expand to each be one third the width of the TableLayout ("ButtonMenu")? Note that in the "real" case, there will be a variable number of buttons, not always 3.