Set Margin on RecyclerView programmatically

前端 未结 4 768
旧巷少年郎
旧巷少年郎 2021-01-12 00:13

I need to set the top margin on a RecyclerView programmatically, but I get this exception:

java.lang.RuntimeException: Unable to resume activity java.lang.Cla

4条回答
  •  无人共我
    2021-01-12 00:53

    I am also facing this type of issue, Use below code to handle margin issue of recyclerview.

        ViewGroup.MarginLayoutParams marginLayoutParams=
        (ViewGroup.MarginLayoutParams) recyclerView.getLayoutParams();
    
        // here i try to set only top margin, Get dimension from dimension file.
        int topMargin =  getResources()
        .getDimensionPixelSize(R.dimen.top_margin);
    
        marginLayoutParams.setMargins(marginLayoutParams.getMarginStart(),
        topMargin, marginLayoutParams.getMarginEnd(), marginLayoutParams.bottomMargin);
    
        recyclerView.setLayoutParams(marginLayoutParams);
    

提交回复
热议问题