我正在尝试创建一个由 15x20 TextView 组成的 GridView ,并且我希望 TextView 从下到上填充。例如 getChildAt(0) 将引用左下角的网格。现在它总是引用左上角的网格。 我试过了
android:stackFromBottom="true"
但它不起作用。下面是我的代码,希望有人能给我一些见解我做错了什么。谢谢!
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sample_main_layout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="313dp"
android:layout_weight="0.18">
<GridLayout
android:id="@+id/map_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:columnCount="15"
android:descendantFocusability="afterDescendants"
android:gravity="center"
android:horizontalSpacing="30dp"
android:paddingLeft="20dp"
android:rowCount="20"
android:stackFromBottom="true"
android:stretchMode="columnWidth"
android:verticalSpacing="30dp">
</GridLayout>
使用你们建议的一些解决方案,我创建了一个函数来更正我的网格索引。它将从最后一个位置引用网格。
编辑:
public int correctIndex(int index) {
int mul = (int) Math.floor(index /15);
int remainder = index%15;
remainder = 15-remainder;
int correctedIndex = 300-(mul*15+remainder);
return correctedIndex;
}
最佳答案
您不需要以相反的顺序填充GridView
。您只需要以相反的顺序读取它即可。
代替:
gridView.getChildAt(position);
使用:
gridView.getChildAt(gridView.getChildCount() - position - 1);
当然,你应该用你正在使用的变量替换位置 - 1
关于从底部开始的 Android gridview 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611824/