Set drawable for DividerItemDecoration

后端 未结 4 1367
感动是毒
感动是毒 2021-02-02 05:28

I\'m trying to set my custom drawable (line) for DividerItemDecoration, but with no success. Where is the mistake?

DividerItemDecoration dividerItemDecoration =          


        
相关标签:
4条回答
  • 2021-02-02 06:17

    Solution (Programmatically):

    If you just want to change the color for the dividers instead of creating a custom drawable you can use a ColorDrawable:

    DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
    itemDecoration.setDrawable(new ColorDrawable(R.color.greycc));
    recyclerView.addItemDecoration(itemDecoration);
    

    If the size matters in addition to colors you can use a GradientDrawwable:

    DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
    
    GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{0xfff7f7f7, 0xfff7f7f7});
    drawable.setSize(1,1);
    itemDecoration.setDrawable(drawable);
    
    recyclerView.addItemDecoration(itemDecoration);
    

    Please note that setting the color values in the array requires a full octet of hex values, otherwise incorrect colors will be shown i.e., 0xFF3E3E3E in opposed to 0X3E3E3E.

    0 讨论(0)
  • 2021-02-02 06:21

    Change the shape to rectangle.

    Ex:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
        <size
            android:width="1dp"
            android:height="1dp" />
        <solid android:color="@color/primary" />
    </shape>
    
    0 讨论(0)
  • 2021-02-02 06:21

    If you want to change divider color, you can change it from you AppTheme by adding this line
    <item name="android:listDivider">@color/your_color</item>

    0 讨论(0)
  • 2021-02-02 06:29
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
                    LinearLayoutManager.HORIZONTAL);
    dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.line_decoration));
            recyclerView.addItemDecoration(dividerItemDecoration);
            DividerItemDecoration dividerItemDecorationVertical = new DividerItemDecoration(getContext(),
                    LinearLayoutManager.VERTICAL);
    
            dividerItemDecorationVertical.setDrawable(getContext().getResources().getDrawable(R.drawable.line_decoration));
            recyclerView.addItemDecoration(dividerItemDecorationVertical);
    
    0 讨论(0)
提交回复
热议问题