我正在尝试使用 GridLayout(不是 GridView!)来实现这样的布局:
但是使用我的代码(以及到目前为止我尝试过的所有内容),两个水平相邻视图的顶部总是对齐的。是否可以告诉每个新视图与其列的高水位线对齐?(请参阅“自动索引分配”下的新布局小部件:Space 和 GridLayout )
我的 layout.xml 现在看起来像这样:
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:columnCount="2"
/>
在这个 GridLayout 中,我以编程方式添加我的视图:
GridLayout scrollViewContainer = Views.findById(rootView, R.id.scroll_view_container);
for (i=0; i < list.size(); i++)
TextView tv = new TextView(context);
tv.setText("foobar");
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
// left, right, left, right - creates new default rows
lp.columnSpec = GridLayout.spec(i%2);
// this would put every view in the same row => all Views are on the very top
// lp.rowSpec = GridLayout.spec(0);
tv.setLayoutParams(lp);
scrollViewContainer.addView(tv);
}