大佬教程收集整理的这篇文章主要介绍了android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
CardViewRow.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://scheR_800_11845@as.android.com/apk/res/android" xmlns:card_view="http://scheR_800_11845@as.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="vertical" card_view:cardCornerRadius="0dp" card_view:cardUseCompatPadding="true" > <LinearLayout android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:BACkground="@drawable/card_SELEctor" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/thumbnail" android:layout_width="300dp" android:layout_height="150dp" android:scaleType="centerCrop"/> <LinearLayout android:weightSum="2" android:padding="5dp" android:layout_gravity="bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:BACkground="#80000000"> <TextView android:id="@+id/size_txt" android:layout_weight="1" android:textSize="12sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/silver" android:gravity="left"/> <TextView android:layout_weight="1" android:gravity="right" android:textColor="@color/silver" android:id="@+id/time_txt" android:textSize="12sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <View android:id="@+id/SELEctor" android:visibility="invisible" android:BACkground="#950096ff" android:layout_width="match_parent" android:layout_height="match_parent"> </View> </FrameLayout> <TextView android:id="@+id/title_txt" android:padding="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="5" /> </LinearLayout> </android.support.v7.widget.CardView>
Displaymetrics metrics = new Displaymetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int cardWidth =(int) metrics.xdpi; //CardWidth==1Inch. int spans = (int) Math.floor(mRecyclerView.getContext().getresources().getDisplaymetrics().widthPixels / (float) cardWidth); sglm.setSpanCount(spans);
/ *这完全适用于我的nexus 5和三星标签,但在我的lenovo手机上,卡宽度小于1.5CM,跨度计数超出我的预期.* /
int spans = (int) Math.floor(recyclerView.getContext().getresources().getDisplaymetrics().widthPixels / (float) cardWidth); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if(layoutManager instanceof StaggeredGridLayoutManager) { ((StaggeredGridLayoutManager) layoutManager).setSpanCount(spans); }
然后使用RecyclerView.ItemDecoration在跨距之间添加间距
以上是大佬教程为你收集整理的android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?全部内容,希望文章能够帮你解决android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。