styles.xml做如下修改:
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- 修改分割线样式-->
<item name="android:listDivider">@drawable/s</item>
</style>
@draweble/s代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:centerColor="#ff00ff00"
android:endColor="#ff0000ff"
android:startColor="#ffff0000"
android:type="linear" />
<size android:height="4dp"/>
</shape>
MyItemDecoration代码如下:
package com.wuzhiqiang.learnswiperefreshlayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by mfpc on 2015/11/2.
*/
public class MyItemDecoration extends RecyclerView.ItemDecoration
{
private static final int[] ATTRS = {android.R.attr.listDivider};
private Drawable mDivider;
public MyItemDecoration(Context context)
{
TypedArray array = context.obtainStyledAttributes(ATTRS);
// 获取分隔条
mDivider = array.getDrawable(0);
array.recycle();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
{
super.onDrawOver(c, parent, state);
int count = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth()-parent.getPaddingRight();
for(int i = 0; i < count; i++)
{
View v = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) v.getLayoutParams();
int top = v.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
}
}
}
但是中间出错了:
@drawabel/s文件应该怎么写?或者使其他地方有问题?
应为 RecyclerView 和 LayoutManager 是分离的,所以没有分割线属性,可以用 RecyclerView.ItemDecoration 进行分割线的绘制。
提供一个简单的装饰
使用方法