android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?

android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?,第1张

概述我正在使用垂直StaggeredGridLayoutManager来显示一些缩略图.每行包含1英寸宽和150度高的卡片视图.我的问题是如何设置交错网格跨度计数以使用屏幕的可用物理宽度? CardViewRow.xml <?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="h 我正在使用垂直StaggeredGrIDLayoutManager来显示一些缩略图.每行包含1英寸宽和150度高的卡片视图.我的问题是如何设置交错网格跨度计数以使用屏幕的可用物理宽度?

CardVIEwRow.xml

<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.CardVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:card_vIEw="http://schemas.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>

目前我正在使用此代码. (致谢:mstrengis)

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 – 如何设置交错网格跨度计数以使用可用的屏幕宽度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1135116.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存