我尝试使用方形图像制作网格布局.我认为必须可以通过 *** 作onMeasure来 *** 作GrIDLayoutManager
super.onMeasure(recycler, state, wIDthSpec, wIDthSpec);
代替
super.onMeasure(recycler, state, wIDthSpec, heightSpec);
但不幸的是,这没有用.
有任何想法吗?
解决方法:
为了在我的RecyclerVIEw中使用方形元素,我为我的根视图元素提供了一个简单的包装器;我使用以下SquarerelativeLayout代替relativeLayout.
package net.simplyadvanced.Widget;import androID.content.Context;import androID.util.AttributeSet;import androID.Widget.relativeLayout;/** A relativeLayout that will always be square -- same wIDth and height, * where the height is based off the wIDth. */public class SquarerelativeLayout extends relativeLayout { public SquarerelativeLayout(Context context) { super(context); } public SquarerelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public SquarerelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetAPI(VERSION_CODES.LolliPOP) public SquarerelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { // Set a square layout. super.onMeasure(wIDthMeasureSpec, wIDthMeasureSpec); }}
然后,在我的适配器的XML布局中,我刚刚引用了自定义视图,如下所示.不过,您也可以通过编程方式执行此 *** 作.
<?xml version="1.0" enCoding="utf-8"?><net.simplyadvanced.Widget.SquarerelativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/elementRootVIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <!-- More Widgets here. --></net.simplyadvanced.Widget.SquarerelativeLayout>
注意:根据网格的方向,您可能希望宽度基于高度(GrIDLayoutManager.HORIZONTAL),而不是基于宽度的高度(GrIDLayoutManager.VERTICAL).
总结以上是内存溢出为你收集整理的android – GridLayoutManager for RecyclerView的Square布局全部内容,希望文章能够帮你解决android – GridLayoutManager for RecyclerView的Square布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)