我正在尝试构建一个涉及 RecyclerView 或 GridView 的 UI,我想在其中根据动态填充的项目的数量和宽度自动设置列数。
这是我到目前为止所取得的成就的形象......
这是我想要实现的目标的图像...
我的项目布局 xml
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@drawable/hollow_white_background"
android:id="@+id/foodItemName"
android:text="PIZZA"
android:gravity="center"
android:textColor="@color/colorGray300"
android:textStyle="bold"
android:textSize="15sp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
请帮忙!
最佳答案
在您的 xml 文件中,为您的 GridView 添加 android:numColumns="auto_fit"和 android:stretchmode="columnWidth"属性。第一个属性根据屏幕大小决定应该有多少列,第二个属性决定如何处理剩余的额外空间,在这种情况下会增加列宽。
关于android - LayoutManager for RecyclerView 根据项目的宽度自动设置列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35240309/