OK, I spent some hours figuring out what's going on here. Well, I didn't find any working way to set columnSpan or rowSpan at runtime.
But I found a solution that works (at least for me):
Java code
private LinearLayout addNewSpannedView(Integer resourceId, ViewGroup rootElement) {
return (LinearLayout) ((ViewGroup) getLayoutInflater().inflate(resourceId, rootElement, true)).getChildAt(rootElement.getChildCount() - 1);
}
// set columnSpan depending on some logic (gridLayout is the layout to add the view's to -> in my case these are LinearLayouts)
shape = addNewSpannedView(columnSpan == 1 ? R.layout.grid_ll_col_span_1 : R.layout.grid_ll_col_span_2, gridLayout);
grid_ll_col_span_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/shapeWidth"
android:layout_height="wrap_content"
android:layout_columnSpan="2"/>
Hint: It's very important, that you set the width and height attribute, before inflate() adds the view to the root element (i.e. the parent element).
I hope, someone can use this ;-)