我有一个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>
分隔符获得我想要的颜色,但不是不受影响的厚度。我希望分隔线是一个不同高度的矩形..如何设置高度/厚度?
答案 0 :(得分:0)
导航抽屉布局
<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);
这是itemDecorator类
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>
答案 1 :(得分:0)
您应该在代码size
中使用代码thickness
而不是shape
。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:height="1dp" />
<solid android:color="#10000000"/>
</shape>