我想让recyclerview itme在任何屏幕上保持相同的宽高比(宽高比为8:5);那么如何动态设置RecyclerView项目的高度?
答案 0 :(得分:1)
如果要以编程方式设置其宽度和高度,则需要使用ViewGroup.LayoutParams
ViewGroup.LayoutParams layoutParams= recyclerview.getLayoutParams();
layoutParams.height = height;
layoutParams.widht = width
recyclerview.setLayoutParams(layoutParams);
您需要根据自己的需要将height
和width
传递给它。
我使用ViewGroup.LayoutParams
,但您可以使用您放置recyclerview的任何ViewGroup。如果它在LinearLayout中,您将使用LinearLayout.LayoutParams等等。
让我知道它是否适合你,并记得如果有的话,赞成/选择答案正确,欢呼
答案 1 :(得分:1)
您可以使用https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html
这里是示例:添加此实现
'com.google.android:flexbox:2.0.1'
到您的依赖项。
这是您的reyclerview配置:
val layoutManager = FlexboxLayoutManager(this)
layoutManager.flexDirection = FlexDirection.ROW
layoutManager.justifyContent = JustifyContent.FLEX_START
recyclerview.layoutManager = layoutManager
您也可以https://gist.github.com/siifii/3edd8cd2d4f5a9b4672b9976c0c55ff4
检查此要点