Android: ListView, problem with rounded corners

有些话、适合烂在心里 提交于 2019-11-30 23:46:35
public class ClippedListView extends ListView {

/**
 * @param context
 */
public ClippedListView(Context context) {
    super(context);
}

/**
 * @param context
 * @param attrs
 */
public ClippedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    float radius = 10.0f;
    Path clipPath = new Path();
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
    clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
    canvas.clipPath(clipPath);
    super.dispatchDraw(canvas);
}
}

First clipping was not working, then using

setLayerType(View.LAYER_TYPE_SOFTWARE, null)

on my clippedListView solve the issue. My items' background are not messing with my corners anymore!

Well, if you need this background for the items in the list, you should attach the background to the item layout, and not to the list view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!