Android Gridlayout:始终在高水位线上添加新视图
我尝试使用GridLayout(不是GridView!)来实现如下布局: 但在我的代码中(以及迄今为止我所尝试的一切),两个水平相邻视图的顶部总是对齐的。是否可以告诉每个新视图在其列的高水位线上对齐?(见“自动索引分配”下) 我的layout.xml现在看起来像这样:Android Gridlayout:始终在高水位线上添加新视图,android,android-layout,android-gridlayout,Android,Android Layout,Android Gridlayout,我尝试使用GridLayout(不是GridView!)来实现如下布局: 但在我的代码中(以及迄今为止我所尝试的一切),两个水平相邻视图的顶部总是对齐的。是否可以告诉每个新视图在其列的高水位线上对齐?(见“自动索引分配”下) 我的layout.xml现在看起来像这样: <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll_
<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);
}
GridLayout scrollViewContainer=Views.findById(rootView,R.id.scroll\u view\u container);
对于(i=0;i所有视图都在最上面
//lp.rowSpec=GridLayout.spec(0);
tv.setLayoutParams(lp);
scrollViewContainer.addView(电视);
}
更新:
使用新的和,您现在可以实现这一点,而无需第三方LIB
旧答案:
我发现这种观点已经被谷歌以StaggedGridView的名义提出了。它曾经在AOSP中,但再次被取出(我丢失了此信息的来源…但无论如何)。但您可以在此处找到StaggeredGridView的修改版本:
谷歌的原始来源如下:
另一个很好的实现是在官方的Etsy应用程序中。该守则已公布:
我的代码与您的代码相似,我遇到了相同的问题。问题是在创建layoutParams后,将列和行规格直接设置为layoutParams。您应该在构造函数上指定此属性
Spec rowspecs = GridLayout.spec(row, 1);
Spec colspecs = GridLayout.spec(column, 1);
GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(rowspecs, colspecs);
什么是行,什么是列?这些是什么类型的对象?