设置 NavigationView 的列表分隔高度

Setting NavigationView's list divider height

我有一个 NavigationView,我正在尝试为其设置自定义列表分隔符。

我制作了文件"drawer_list_divider.xml":

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle"
android:thickness="25dp">
<solid android:color="@color/secondaryBackground"/>

我是这样设置的:android:theme="@style/NavigationViewTheme"

风格:

<style name="NavigationViewTheme" > <item name="android:listDivider">@drawable/drawer_list_divider</item> </style>

分隔板得到我想要的颜色,但厚度不受影响。我希望分隔线是一个不同高度的矩形。如何设置 height/thickness?

导航抽屉布局

<android.support.v4.widget.DrawerLayout  
 android:id="@+id/drawerLayout"  
 android:layout_width="match_parent"  
 android:layout_height="match_parent"
 android:layout_below="@+id/appBar">  
 <!--Content-->  
 <LinearLayout  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical">  
   <TextView  
     android:id="@+id/content_textView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:text="@string/app_name" />  
 </LinearLayout>  
 <!-- Navigation Drawer-->  
 <android.support.v7.widget.RecyclerView  
   android:id="@+id/drawerRecyclerView"  
   android:layout_width="300dp"  
   android:layout_height="match_parent"  
   android:layout_gravity="left"  
   android:background="#ffffff">  
 </android.support.v7.widget.RecyclerView>  

然后设置一个recyclerview适配器

   public class DrawerAdapter extends 
  RecyclerView.Adapter<DrawerAdapter.DrawerViewHolder> {  
 private ArrayList<DrawerItem> drawerMenuList;  
   public DrawerAdapter(ArrayList<DrawerItem> drawerMenuList) {  
 this.drawerMenuList = drawerMenuList;  
 }  
@Override  
public DrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
 View view;  
 view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);  
 return new DrawerViewHolder(view);  
}  
 @Override  
 public void onBindViewHolder(DrawerViewHolder holder, int position) {  
 holder.title.setText(drawerMenuList.get(position).getTitle());  
 holder.icon.setImageResource(drawerMenuList.get(position).getIcon());  
 }  
 @Override  
 public int getItemCount() {  
 return drawerMenuList.size();  
 }  
 class DrawerViewHolder extends RecyclerView.ViewHolder {  
 TextView title;  
 ImageView icon;  
 public DrawerViewHolder(View itemView) {  
   super(itemView);  
   title = (TextView) itemView.findViewById(R.id.title);  
   icon = (ImageView) itemView.findViewById(R.id.icon);  
 }      }      }  

然后设置要显示导航抽屉的适配器。设定时 recyclerview 设置分隔符

 DrawerAdapter adapter = new DrawerAdapter(mDrawerItemList);  
 mRecyclerView.setLayoutManager(new LinearLayoutManager(this));  
 mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
 mRecyclerView.setAdapter(adapter);  

这是itemDecoratorclass

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;

public SimpleDividerItemDecoration(Context context) {
    mDivider = context.getResources().getDrawable(R.drawable.recycler_horizontal_divider);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}}

这是可绘制的分隔线。

  <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
    android:width="1dp"
    android:height="1dp" />

<solid android:color="#2EC590" />

</shape>

您应该在标签 shape 中使用标签 size 而不是 thickness

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <size android:height="1dp" />

    <solid android:color="#10000000"/>

</shape>