我试图实现 GridView 的第一列比其他四列宽一些:
正如您在屏幕截图中看到的,第一列的宽度不足以显示整个日期而不换行。由于其他四列中还留有一些空间,因此我想缩小它们并给第一列多一点宽度。
目前,我正在使用 ArrayAdapter 和 simple_list_item_1 来实现当前的结果。
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
testView.setAdapter(adapter);
我的 GridView XML 如下所示:
<GridView
android:id="@+id/testView"
android:background="#eee"
android:numColumns="5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="2dp"
app:layout_constraintTop_toBottomOf="@+id/button3"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp" />
如何以合理的方式解决这个问题?
编辑: 我不想像可能的重复项那样更改所有列的大小。我只想改变第一个的大小。
最佳答案
利用跨度大小来获取所需的列大小。
GridLayoutManager manager = new GridLayoutManager(this, 10); // 3 cols of 3 + 1 col of 1
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position % 4 == 3)
return 1; // Fourth column is 1x
else
return 3; // Remaining columns are 3x
}
});
recyclerView.setLayoutManager(manager);
这只是使用 span 的示例,但不是您场景的解决方案。
希望这对您有帮助。
关于java - 更改 GridView 第一个列的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46468199/